Moved BlockCycle logic to generic class

This commit is contained in:
Elias Müller 2023-12-09 00:09:46 +01:00
parent 848c3f02fe
commit b72a5947b4
2 changed files with 56 additions and 26 deletions

View File

@ -7,6 +7,7 @@ import eu.mhsl.craftattack.spawn.appliances.projectStart.command.ProjectStartCom
import eu.mhsl.craftattack.spawn.appliances.projectStart.command.ProjectStartResetCommand; import eu.mhsl.craftattack.spawn.appliances.projectStart.command.ProjectStartResetCommand;
import eu.mhsl.craftattack.spawn.appliances.projectStart.listener.PlayerInvincibleListener; import eu.mhsl.craftattack.spawn.appliances.projectStart.listener.PlayerInvincibleListener;
import eu.mhsl.craftattack.spawn.config.Configuration; import eu.mhsl.craftattack.spawn.config.Configuration;
import eu.mhsl.craftattack.spawn.util.BlockCycle;
import eu.mhsl.craftattack.spawn.util.ComponentUtil; import eu.mhsl.craftattack.spawn.util.ComponentUtil;
import eu.mhsl.craftattack.spawn.util.Countdown; import eu.mhsl.craftattack.spawn.util.Countdown;
import eu.mhsl.craftattack.spawn.util.IteratorUtil; import eu.mhsl.craftattack.spawn.util.IteratorUtil;
@ -14,32 +15,37 @@ import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import static java.util.Map.entry; import static java.util.Map.entry;
import static org.bukkit.Sound.MUSIC_DISC_PIGSTEP; import static org.bukkit.Sound.MUSIC_DISC_PIGSTEP;
public class ProjectStart extends Appliance { public class ProjectStart extends Appliance {
private final Countdown countdown = new Countdown(localConfig().getInt("countdown"), this::format, this::announce, this::projectStart);
private final int startMusicAt = 137; private final int startMusicAt = 137;
private int cycleState = 0; private final Location glassLocation = new Location(Bukkit.getWorld("world"), -224, 67, -368);
private final World world = Bukkit.getWorld("world"); private final Countdown countdown = new Countdown(
private final List<Material> glassColors = List.of( localConfig().getInt("countdown"),
this::format,
this::announce,
this::startProject
);
private final BlockCycle blockCycle = new BlockCycle(
glassLocation,
Material.ORANGE_STAINED_GLASS,
List.of(
Material.RED_STAINED_GLASS, Material.RED_STAINED_GLASS,
Material.YELLOW_STAINED_GLASS, Material.YELLOW_STAINED_GLASS,
Material.GREEN_STAINED_GLASS, Material.GREEN_STAINED_GLASS,
Material.BLUE_STAINED_GLASS Material.BLUE_STAINED_GLASS
)
); );
private final Material defaultGlassColor = Material.ORANGE_STAINED_GLASS;
private final Location glassLocation = new Location(world, -224, 67, -368);
//
private final Map<GameRule<Boolean>, Boolean> gameRulesAfterStart = Map.ofEntries( private final Map<GameRule<Boolean>, Boolean> gameRulesAfterStart = Map.ofEntries(
entry(GameRule.DO_DAYLIGHT_CYCLE, true), entry(GameRule.DO_DAYLIGHT_CYCLE, true),
entry(GameRule.DO_INSOMNIA, true), entry(GameRule.DO_INSOMNIA, true),
@ -60,10 +66,9 @@ public class ProjectStart extends Appliance {
this.countdown.addCustomAnnouncement( this.countdown.addCustomAnnouncement(
new Countdown.CustomAnnouncements( new Countdown.CustomAnnouncements(
counter -> counter == startMusicAt, counter -> counter == startMusicAt,
counter -> { counter -> glassLocation
Objects.requireNonNull(world) .getWorld()
.playSound(glassLocation, MUSIC_DISC_PIGSTEP, SoundCategory.RECORDS, 500f, 1f); .playSound(glassLocation, MUSIC_DISC_PIGSTEP, SoundCategory.RECORDS, 500f, 1f)
}
) )
); );
} }
@ -78,7 +83,7 @@ public class ProjectStart extends Appliance {
} }
private void announce(Component message) { private void announce(Component message) {
cycleBeacon(); blockCycle.next();
IteratorUtil.onlinePlayers(player -> player.sendMessage(message)); IteratorUtil.onlinePlayers(player -> player.sendMessage(message));
} }
@ -92,7 +97,7 @@ public class ProjectStart extends Appliance {
this.restoreBeforeStart(); this.restoreBeforeStart();
} }
public void projectStart() { public void startProject() {
setEnabled(false); setEnabled(false);
IteratorUtil.worlds(World::getWorldBorder, worldBorder -> worldBorder.setSize(worldBorder.getMaxSize())); IteratorUtil.worlds(World::getWorldBorder, worldBorder -> worldBorder.setSize(worldBorder.getMaxSize()));
@ -119,12 +124,14 @@ public class ProjectStart extends Appliance {
) )
); );
glassLocation.getBlock().setType(defaultGlassColor); blockCycle.reset();
} }
public void restoreBeforeStart() { public void restoreBeforeStart() {
setEnabled(true); setEnabled(true);
IteratorUtil.onlinePlayers(Player::stopAllSounds);
IteratorUtil.worlds(World::getWorldBorder, worldBorder -> { IteratorUtil.worlds(World::getWorldBorder, worldBorder -> {
worldBorder.setSize(localConfig().getLong("worldborder-before")); worldBorder.setSize(localConfig().getLong("worldborder-before"));
worldBorder.setWarningDistance(0); worldBorder.setWarningDistance(0);
@ -133,13 +140,7 @@ public class ProjectStart extends Appliance {
IteratorUtil.worlds(world -> world, world -> IteratorUtil.setGameRules(gameRulesAfterStart, true)); IteratorUtil.worlds(world -> world, world -> IteratorUtil.setGameRules(gameRulesAfterStart, true));
glassLocation.getBlock().setType(defaultGlassColor); blockCycle.reset();
}
private void cycleBeacon() {
glassLocation.getBlock().setType(glassColors.get(cycleState));
cycleState++;
if(cycleState >= glassColors.size()) cycleState = 0;
} }
public boolean isEnabled() { public boolean isEnabled() {

View File

@ -0,0 +1,29 @@
package eu.mhsl.craftattack.spawn.util;
import org.bukkit.Location;
import org.bukkit.Material;
import java.util.List;
public class BlockCycle {
private final Location location;
private final Material defaultBlock;
private final List<Material> blockList;
private int current = 0;
public BlockCycle(Location location, Material defaultBlock, List<Material> blockList) {
this.location = location;
this.defaultBlock = defaultBlock;
this.blockList = blockList;
}
public void next() {
location.getBlock().setType(blockList.get(current));
current++;
if(current >= blockList.size()) current = 0;
}
public void reset() {
location.getBlock().setType(defaultBlock);
}
}