diff --git a/src/main/java/org/kapelle/hardcore_revive/HardcoreRevive.java b/src/main/java/org/kapelle/hardcore_revive/HardcoreRevive.java index 00eaddb..9dd0b0a 100644 --- a/src/main/java/org/kapelle/hardcore_revive/HardcoreRevive.java +++ b/src/main/java/org/kapelle/hardcore_revive/HardcoreRevive.java @@ -5,12 +5,14 @@ import org.bukkit.plugin.java.JavaPlugin; public class HardcoreRevive extends JavaPlugin { DropHead dropHead; SummonHead summonHead; + RevivePlayer revivePlayer; @Override public void onEnable() { if(this.getServer().isHardcore()){ dropHead = new DropHead(this); summonHead = new SummonHead(this); + revivePlayer = new RevivePlayer(this); this.getLogger().info("hardcore-revive plugin enabled."); }else{ this.getLogger().info("Not running in hardcore mode. hardcore-revive disabled."); diff --git a/src/main/java/org/kapelle/hardcore_revive/RevivePlayer.java b/src/main/java/org/kapelle/hardcore_revive/RevivePlayer.java new file mode 100644 index 0000000..3195477 --- /dev/null +++ b/src/main/java/org/kapelle/hardcore_revive/RevivePlayer.java @@ -0,0 +1,63 @@ +package org.kapelle.hardcore_revive; + +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.block.Block; +import org.bukkit.block.Skull; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.util.Vector; + +public class RevivePlayer implements Listener { + private JavaPlugin plugin; + + public RevivePlayer(JavaPlugin plugin){ + this.plugin = plugin; + + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onBlockPlace(BlockPlaceEvent event){ + if(event.getBlockPlaced().getType() == Material.PLAYER_HEAD && !event.isCancelled()){ + if(isValidMultiblock(event.getBlockPlaced().getLocation())){ + OfflinePlayer spirit = getPlayerFromHead(event.getBlockPlaced()); + if (spirit.isOnline() /* && spirit.getPlayer().getGameMode() == GameMode.SPECTATOR */){ + revivePlayer(spirit.getPlayer(),event.getBlockPlaced().getLocation()); + }else{ + // Player is not online or not dead + plugin.getServer().broadcastMessage("Player not ded"); + // TODO + } + } + } + } + + private boolean isValidMultiblock(Location lastPlacedBlock){ + return lastPlacedBlock.clone().add(new Vector(0,-1,0)).getBlock().getType() == Material.GOLD_BLOCK + && lastPlacedBlock.clone().add(new Vector(0,-2,0)).getBlock().getType() == Material.MAGMA_BLOCK; + } + + private OfflinePlayer getPlayerFromHead(Block head){ + Skull skull = (Skull) (head.getState()); + return skull.getOwningPlayer(); + } + + private void revivePlayer(Player player, Location headLocation){ + // Break blocks + headLocation.getBlock().setType(Material.AIR); + headLocation.add(new Vector(0,-1,0)).getBlock().setType(Material.AIR); + headLocation.add(new Vector(0,-1,0)).getBlock().setType(Material.AIR); + + // Teleport player + player.teleport(headLocation); + + // Set player gamemode + player.setGameMode(GameMode.SURVIVAL); + } +}