lifesteal/src/ca/thetechrobo/smp/DeathListener.java

131 lines
4.4 KiB
Java

package ca.thetechrobo.smp;
import java.util.ArrayList;
import org.bukkit.Sound;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
public class DeathListener implements Listener {
public ScoreboardWrapper scores;
private ArrayList<String> skulllore;
@EventHandler
public void onDeath(PlayerDeathEvent e) {
this.scores.dieHearts(e.getEntity(), 4);
ItemStack skull = rethead(e.getEntity());
e.getEntity().getWorld().dropItemNaturally(e.getEntity().getLocation(), skull);
}
public ItemStack rethead(OfflinePlayer p) {
ItemStack skull = new ItemStack(Material.PLAYER_HEAD);
SkullMeta meta = (SkullMeta) skull.getItemMeta();
meta.setOwningPlayer(p);
meta.setLore(this.skulllore);
ItemMeta newmeta = this.scores.save(meta, 69, "IsDroppedSkull");
skull.setItemMeta(newmeta);
return skull;
}
public ItemStack rethead(Player p) {
return rethead((OfflinePlayer) p);
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
this.scores.addPlayerToScoreboard(e.getPlayer());
// v ITEM DUPING PREVENTION
boolean banned = (this.scores.load(e.getPlayer(), "bannedByLSPLUGIN", 0, false) == 1);
if (banned) {
e.getPlayer().getInventory().clear();
Bukkit.getLogger().info("Player was UNBANNED after being BANNED.");
this.scores.save(e.getPlayer(), 0, "bannedByLSPLUGIN");
}
// ^ ITEM DUPING PREVENTION
}
@SuppressWarnings("deprecation")
@EventHandler
public void onPlayerUse(PlayerInteractEvent e) {
Player p = e.getPlayer();
ItemStack i = p.getInventory().getItemInMainHand();
if (!(i.hasItemMeta())) return;
boolean rightclickair = (e.getAction() == Action.RIGHT_CLICK_AIR);
if (!rightclickair) {
return;
}
boolean heartblock = (i.getType() == Material.PLAYER_HEAD && rightclickair);
if (heartblock) {
this.scores.dieHearts(p, -4.0);
ItemStack newi = i.clone();
newi.setAmount(1);
p.getInventory().removeItem(newi);
return;
}
if (!(i.getItemMeta().hasDisplayName())) {
return;
}
Life d = new Life();
d.create();
boolean condition1a = (i.getItemMeta().getDisplayName().equals(d.dn));
// DO NOT CHANGE THIS TO
// i.getItemMeta().displayName().equals(Component.text(d.dn))
// IT DOES NOT WORK. I am sick of figuring out why.
// It caused many minutes of frustration at the computer.
// Just... don't.
// Unless you're ready to deal with insanity.
boolean condition1b = (i.getType() == d.m);
boolean lifeblock = (condition1a && condition1b);
if (lifeblock) {
this.scores.die(p, -1);
p.getInventory().removeItem(d.item);
return;
}
LifeExtractor f = new LifeExtractor();
f.create();
if ((i.getType() == f.m) && (i.getItemMeta().getDisplayName().equals(f.dn))) {
if (p.getInventory().firstEmpty() == (-1)) {
p.sendMessage("§4Not enough room in your inventory.");
p.playSound(p, Sound.ENTITY_ENDERMAN_TELEPORT, 76, 0);
return;
}
if (this.scores.die(p, 0) == 1) {
p.sendMessage("§4Performing this operation will cause you to get banned. Cancelled.");
p.playSound(p, Sound.ENTITY_ENDERMAN_TELEPORT, 69, 0);
return;
}
this.scores.die(p, 1);
d.add(p);
}
HeartExtratrator he = new HeartExtratrator();
he.create();
if ((i.getType() == he.m) && (i.getItemMeta().getDisplayName().equals(he.dn))) {
if (p.getInventory().firstEmpty() == (-1)) {
p.sendMessage("§4Not enough room in your inventory.");
p.playSound(p, Sound.ENTITY_ENDERMAN_TELEPORT, 76, 0);
return;
}
if (this.scores.dieHearts(p, 0) <= 8.0) {
p.sendMessage("§4Performing this operation will cause you to lose a life. Cancelled.");
p.playSound(p, Sound.ENTITY_ENDERMAN_TELEPORT, 69, 0);
return;
}
this.scores.dieHearts(p, 8);
p.getInventory().addItem(rethead(p));
}
}
public void initialise(LifestealPlugin pl) {
this.scores = new ScoreboardWrapper();
this.scores.initialise(pl);
this.skulllore = new ArrayList<String>();
this.skulllore.add("Can be used as a Life.");
}
}