diff --git a/src/main/java/rip/tilly/bedwars/customvillager/CustomVillager.java b/src/main/java/rip/tilly/bedwars/customvillager/CustomVillager.java new file mode 100644 index 0000000..b73f5f1 --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/customvillager/CustomVillager.java @@ -0,0 +1,46 @@ +package rip.tilly.bedwars.customvillager; + +import net.minecraft.server.v1_8_R3.DamageSource; +import net.minecraft.server.v1_8_R3.EntityVillager; +import net.minecraft.server.v1_8_R3.NBTTagCompound; +import net.minecraft.server.v1_8_R3.World; +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftVillager; +import org.bukkit.event.entity.CreatureSpawnEvent; +import rip.tilly.bedwars.utils.CC; + +public class CustomVillager extends EntityVillager { + + public CustomVillager(World world, String name) { + super(world); + + this.setCustomName(CC.translate(name)); + this.setCustomNameVisible(true); + this.noMove(); + + this.setInvisible(false); + ((CraftVillager) this.getBukkitEntity()).getHandle().setInvisible(false); + + this.world.addEntity(this, CreatureSpawnEvent.SpawnReason.CUSTOM); + } + + public void noMove() { + NBTTagCompound tag = this.getNBTTag(); + if(tag == null) { + tag = new NBTTagCompound(); + } + + this.c(tag); + tag.setInt("NoAI", 1); + this.f(tag); + } + + @Override + public void makeSound(String s, float f, float f1) { + return; + } + + @Override + public boolean damageEntity(DamageSource source, float f) { + return false; + } +} diff --git a/src/main/java/rip/tilly/bedwars/game/Game.java b/src/main/java/rip/tilly/bedwars/game/Game.java index 48e0f43..9492921 100644 --- a/src/main/java/rip/tilly/bedwars/game/Game.java +++ b/src/main/java/rip/tilly/bedwars/game/Game.java @@ -3,17 +3,18 @@ package rip.tilly.bedwars.game; import io.netty.util.internal.ConcurrentSet; import lombok.Getter; import lombok.Setter; -import org.bukkit.Bukkit; +import net.minecraft.server.v1_8_R3.WorldServer; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.block.Block; +import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; -import org.bukkit.entity.Villager; import org.bukkit.inventory.ItemStack; import org.github.paperspigot.Title; import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.customvillager.CustomVillager; import rip.tilly.bedwars.game.arena.Arena; import rip.tilly.bedwars.game.arena.CopiedArena; import rip.tilly.bedwars.generators.Generator; @@ -22,8 +23,8 @@ import rip.tilly.bedwars.generators.GeneratorType; import rip.tilly.bedwars.utils.CC; import rip.tilly.bedwars.utils.CustomLocation; import rip.tilly.bedwars.utils.TimeUtils; + import java.util.*; -import java.util.List; import java.util.stream.Stream; @Getter @@ -38,6 +39,8 @@ public class Game { private final Set runnables = new HashSet<>(); private final Set droppedItems = new ConcurrentSet<>(); + private final Set villagers = new ConcurrentSet<>(); + private final List teams; private final Arena arena; private final GameType gameType; @@ -223,33 +226,33 @@ public class Game { return false; } - public void tick(int amount) { + public void tick(int amount, Game game) { if (this.secondsToMinutes(amount) == 5D) { - Bukkit.broadcastMessage(CC.translate("&bDiamond &egenerators have been upgraded to &bTier II")); + game.broadcast("&bDiamond &egenerators have been upgraded to &bTier II&e."); this.diamondGeneratorTier = GeneratorTier.TWO; } if (this.secondsToMinutes(amount) == 8D) { - Bukkit.broadcastMessage(CC.translate("&aEmerald &egenerators have been upgraded to &bTier II")); + game.broadcast("&aEmerald &egenerators have been upgraded to &bTier II&e."); this.emeraldGeneratorTier = GeneratorTier.TWO; } if (this.secondsToMinutes(amount) == 10D) { - Bukkit.broadcastMessage(CC.translate("&bDiamond &egenerators have been upgraded to &bTier II")); + game.broadcast("&bDiamond &egenerators have been upgraded to &bTier III&e."); this.diamondGeneratorTier = GeneratorTier.THREE; } if (this.secondsToMinutes(amount) == 12D) { - Bukkit.broadcastMessage(CC.translate("&aEmerald &egenerators have been upgraded to &bTier II")); + game.broadcast("&aEmerald &egenerators have been upgraded to &bTier III&e."); this.emeraldGeneratorTier = GeneratorTier.THREE; } if (this.secondsToMinutes(amount) == 15D) { - Bukkit.broadcastMessage(CC.translate("&bDiamond &egenerators have been upgraded to &bTier II")); + game.broadcast("&bDiamond &egenerators have been upgraded to &bTier IV&e."); this.diamondGeneratorTier = GeneratorTier.FOUR; } @@ -281,21 +284,28 @@ public class Game { return seconds / 60D; } - public void spawnVillagers() { - Villager teamAShopVillager = this.arena.getTeamAshop().toBukkitLocation().getWorld().spawn(this.arena.getTeamAshop().toBukkitLocation(), Villager.class); - teamAShopVillager.setCustomName(CC.translate("&aItem Shop")); - teamAShopVillager.setCustomNameVisible(true); + public void spawnVillagers(CopiedArena copiedArena) { + Location teamAshopLoc = copiedArena.getTeamAshop().toBukkitLocation(); + WorldServer worldServer = ((CraftWorld) teamAshopLoc.getWorld()).getHandle(); - Villager teamBShopVillager = this.arena.getTeamBshop().toBukkitLocation().getWorld().spawn(this.arena.getTeamBshop().toBukkitLocation(), Villager.class); - teamBShopVillager.setCustomName(CC.translate("&aItem Shop")); - teamBShopVillager.setCustomNameVisible(true); + CustomVillager teamAshop = new CustomVillager(worldServer, "&aItem Shop"); + teamAshop.setLocation(teamAshopLoc.getBlockX(), teamAshopLoc.getBlockY(), teamAshopLoc.getBlockZ(), teamAshopLoc.getYaw(), teamAshopLoc.getPitch()); - Villager teamAUpgradesVillager = this.arena.getTeamAupgrades().toBukkitLocation().getWorld().spawn(this.arena.getTeamAupgrades().toBukkitLocation(), Villager.class); - teamAUpgradesVillager.setCustomName(CC.translate("&aUpgrades Shop")); - teamAUpgradesVillager.setCustomNameVisible(true); + Location teamBshopLoc = copiedArena.getTeamBshop().toBukkitLocation(); + CustomVillager teamBshop = new CustomVillager(worldServer, "&aItem Shop"); + teamBshop.setLocation(teamBshopLoc.getBlockX(), teamBshopLoc.getBlockY(), teamBshopLoc.getBlockZ(), teamBshopLoc.getYaw(), teamBshopLoc.getPitch()); - Villager teamBUpgradesVillager = this.arena.getTeamBupgrades().toBukkitLocation().getWorld().spawn(this.arena.getTeamBupgrades().toBukkitLocation(), Villager.class); - teamBUpgradesVillager.setCustomName(CC.translate("&aUpgrades Shop")); - teamBUpgradesVillager.setCustomNameVisible(true); + Location teamAupgradeLoc = copiedArena.getTeamAupgrades().toBukkitLocation(); + CustomVillager teamAupgrade = new CustomVillager(worldServer, "&aUpgrades Shop"); + teamAupgrade.setLocation(teamAupgradeLoc.getBlockX(), teamAupgradeLoc.getBlockY(), teamAupgradeLoc.getBlockZ(), teamAupgradeLoc.getYaw(), teamAupgradeLoc.getPitch()); + + Location teamBupgradeLoc = copiedArena.getTeamAupgrades().toBukkitLocation(); + CustomVillager teamBupgrade = new CustomVillager(worldServer, "&aUpgrades Shop"); + teamBupgrade.setLocation(teamBupgradeLoc.getBlockX(), teamBupgradeLoc.getBlockY(), teamBupgradeLoc.getBlockZ(), teamBupgradeLoc.getYaw(), teamBupgradeLoc.getPitch()); + + this.villagers.add(teamAshop); + this.villagers.add(teamBshop); + this.villagers.add(teamAupgrade); + this.villagers.add(teamBupgrade); } } diff --git a/src/main/java/rip/tilly/bedwars/listeners/RandomListeners.java b/src/main/java/rip/tilly/bedwars/listeners/RandomListeners.java index e81e8ed..d84591d 100644 --- a/src/main/java/rip/tilly/bedwars/listeners/RandomListeners.java +++ b/src/main/java/rip/tilly/bedwars/listeners/RandomListeners.java @@ -74,6 +74,10 @@ public class RandomListeners implements Listener { @EventHandler public void onCreatureSpawn(CreatureSpawnEvent event) { + if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.CUSTOM) { + return; + } + if (event.getEntity().getType() != EntityType.ARMOR_STAND && event.getEntity().getType() != EntityType.VILLAGER) { event.setCancelled(true); } diff --git a/src/main/java/rip/tilly/bedwars/listeners/game/GameEndListener.java b/src/main/java/rip/tilly/bedwars/listeners/game/GameEndListener.java index df5ec6f..687055e 100644 --- a/src/main/java/rip/tilly/bedwars/listeners/game/GameEndListener.java +++ b/src/main/java/rip/tilly/bedwars/listeners/game/GameEndListener.java @@ -4,6 +4,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.github.paperspigot.Title; import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.customvillager.CustomVillager; import rip.tilly.bedwars.events.GameEndEvent; import rip.tilly.bedwars.game.Game; import rip.tilly.bedwars.game.GameState; @@ -55,5 +56,9 @@ public class GameEndListener implements Listener { playerData.setGamesPlayed(playerData.getGamesPlayed() + 1); })); + + for (CustomVillager villager : game.getVillagers()) { + villager.die(); + } } } diff --git a/src/main/java/rip/tilly/bedwars/listeners/game/GameStartListener.java b/src/main/java/rip/tilly/bedwars/listeners/game/GameStartListener.java index 205cf6a..cea8b95 100644 --- a/src/main/java/rip/tilly/bedwars/listeners/game/GameStartListener.java +++ b/src/main/java/rip/tilly/bedwars/listeners/game/GameStartListener.java @@ -65,7 +65,7 @@ public class GameStartListener implements Listener { game.getActivatedGenerators().add(emeGen); } - game.spawnVillagers(); + game.spawnVillagers(game.getCopiedArena()); Set gamePlayers = new HashSet<>(); game.getTeams().forEach(team -> team.playingPlayers().forEach(player -> { diff --git a/src/main/java/rip/tilly/bedwars/providers/scoreboard/ScoreboardProvider.java b/src/main/java/rip/tilly/bedwars/providers/scoreboard/ScoreboardProvider.java index a334770..f57ac66 100644 --- a/src/main/java/rip/tilly/bedwars/providers/scoreboard/ScoreboardProvider.java +++ b/src/main/java/rip/tilly/bedwars/providers/scoreboard/ScoreboardProvider.java @@ -266,9 +266,11 @@ public class ScoreboardProvider implements BoardAdapter { lines.add("&7[" + opposingTeam.getPlayerTeam().getChatColor() + opposingTeam.getPlayerTeam().getSmallName() + "&7] &c&l✗"); } lines.add(" "); - lines.add(yourTeam.getPlayerTeam().getChatColor() + yourTeam.playingPlayers().collect(Collectors.toList()).get(0).getName()); + Player teamAplayer = yourTeam.playingPlayers().collect(Collectors.toList()).get(0); + lines.add(yourTeam.getPlayerTeam().getChatColor() + (teamAplayer != null ? teamAplayer.getName() : "None")); lines.add("&7VS"); - lines.add(opposingTeam.getPlayerTeam().getChatColor() + opposingTeam.playingPlayers().collect(Collectors.toList()).get(0).getName()); + Player teamBplayer = opposingTeam.playingPlayers().collect(Collectors.toList()).get(0); + lines.add(opposingTeam.getPlayerTeam().getChatColor() + (teamBplayer != null ? teamBplayer.getName() : "None")); lines.add(" "); lines.add("&dtilly.rip"); lines.add(CC.scoreboardBar); diff --git a/src/main/java/rip/tilly/bedwars/runnables/GameRunnable.java b/src/main/java/rip/tilly/bedwars/runnables/GameRunnable.java index 5181045..417fc41 100644 --- a/src/main/java/rip/tilly/bedwars/runnables/GameRunnable.java +++ b/src/main/java/rip/tilly/bedwars/runnables/GameRunnable.java @@ -35,7 +35,7 @@ public class GameRunnable extends BukkitRunnable { case FIGHTING: this.amount++; this.game.incrementDuration(); - this.game.tick(this.amount); + this.game.tick(this.amount, this.game); break; case ENDING: