package eu.creeper.mc.creeperPit; import eu.creeper.mc.creeperPit.worldguard.Handler; import eu.creeper.mc.creeperPit.worldguard.Region; import eu.creeper.mc.creeperPit.worldguard.WGManager; import org.bukkit.*; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import eu.creeper.mc.creeperPit.config.CFGInit; import java.util.Objects; public final class Main extends JavaPlugin implements Listener { private Location spawnLocation; @Override public void onEnable() { World world = getMainWorld(); this.spawnLocation = new Location(world, -643.5D, 36D, 444.5D, -180F, 0F); Bukkit.getPluginManager().registerEvents(this, this); Bukkit.getPluginManager().registerEvents(new Handler(), this); registerRegions(world); initConfig(); } private World getMainWorld() { World world = Bukkit.getWorld("world"); if (world == null) { world = Bukkit.getWorlds().get(0);//fallback } return world; } private void registerRegions(World world) { Region spawn = new Region("spawn", new Location(world, -659.0D, 36.0D, 402.0D), new Location(world, -629.0D, 55.0D, 448.0D), "Safe"); spawn.setAllowBuild(false); spawn.setAllowBreak(false); spawn.setAllowDrop(false); spawn.setAllowPvp(false); Region spawnReg = new Region("spawnreg", new Location(world, -650.0D, 0.0D, 423.0D), new Location(world, -639.0D, 255.0D, 411.0D), "Safe"); spawnReg.setAllowBuild(false); spawnReg.setAllowBreak(false); spawnReg.setAllowDrop(true); spawnReg.setAllowPvp(false); WGManager.registerRegion(spawn); WGManager.registerRegion(spawnReg); } @EventHandler public void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); event.setJoinMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[+] " + ChatColor.RESET + ChatColor.YELLOW + player.getName()); setupPlayer(player); } @EventHandler public void onQuit(PlayerQuitEvent event) { event.setQuitMessage(ChatColor.RED + "" + ChatColor.BOLD + "[-] " + ChatColor.RESET + ChatColor.YELLOW + event.getPlayer().getName()); } @EventHandler public void onItemDrop(PlayerDropItemEvent event) { Region reg = WGManager.getRegionAt(event.getPlayer().getLocation()); if (reg == null || !reg.canDrop()) event.setCancelled(true); ItemStack item = event.getItemDrop().getItemStack(); Material type = item.getType(); boolean forbiddenArmor = type == Material.IRON_HELMET || type == Material.IRON_CHESTPLATE || type == Material.IRON_LEGGINGS || type == Material.IRON_BOOTS; boolean forbiddenStaff = type == Material.BLAZE_ROD && item.hasItemMeta() && Objects.equals(item.getItemMeta().getDisplayName(), ChatColor.GOLD + "CreeperStaff Manager"); event.setCancelled(forbiddenArmor || forbiddenStaff); } @EventHandler public void onDeath(PlayerDeathEvent event) { Player victim = event.getEntity(); Player killer = victim.getKiller(); event.setDeathMessage(killer != null ? ChatColor.RED + victim.getName() + " got stabbed by " + killer.getName() : ChatColor.RED + victim.getName() + " magically respawned"); event.getDrops().removeIf(item -> { Material type = item.getType(); if (type.name().startsWith("IRON_") || type == Material.BOW || type == Material.FISHING_ROD) return true; return type == Material.BLAZE_ROD && item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equals(ChatColor.GOLD + "CreeperStaff Manager"); }); } @EventHandler public void onRespawn(PlayerRespawnEvent event) { Bukkit.getScheduler().runTaskLater(this, () -> setupPlayer(event.getPlayer()), 5L); } private void setupPlayer(Player player) { player.teleport(spawnLocation); player.setGameMode(GameMode.SURVIVAL); PlayerInventory inv = player.getInventory(); inv.clear(); inv.setHelmet(new ItemStack(Material.IRON_HELMET)); inv.setChestplate(new ItemStack(Material.IRON_CHESTPLATE)); inv.setLeggings(new ItemStack(Material.IRON_LEGGINGS)); inv.setBoots(new ItemStack(Material.IRON_BOOTS)); inv.addItem(new ItemStack(Material.IRON_SWORD), new ItemStack(Material.FISHING_ROD), new ItemStack(Material.BOW), new ItemStack(Material.ARROW, 16), new ItemStack(Material.GOLDEN_APPLE, 2)); player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0, true, false)); player.addPotionEffect(new PotionEffect(PotionEffectType.SATURATION, Integer.MAX_VALUE, 0, true, false)); player.setHealth(20.0); player.setFoodLevel(20); } private void initConfig() { CFGInit cfgInit = new CFGInit(this); cfgInit.itemShop.save("[]"); cfgInit.effectShop.save("[]"); FileConfiguration wgCfgFile = cfgInit.worldguard.get(); ConfigurationSection globalSection = wgCfgFile.getConfigurationSection("global"); if (globalSection == null) { globalSection = wgCfgFile.createSection("global"); //coord globalSection.set("x1", true); globalSection.set("y1", true); globalSection.set("z1", true); globalSection.set("x2", true); globalSection.set("y2", true); globalSection.set("z2", true); //can do x thing globalSection.set("canBuild", true); globalSection.set("canBreak", true); globalSection.set("canPvp", true); globalSection.set("canDrop", true); } cfgInit.worldguard.save(); } }