Skip to content

Commit

Permalink
Minor dupe fix, ability to reload the config in game, fixed /f
Browse files Browse the repository at this point in the history
overriding
  • Loading branch information
Outfenneced committed Jun 25, 2012
1 parent 08c6a2f commit bd97e4f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 78 deletions.
3 changes: 2 additions & 1 deletion .classpath
Expand Up @@ -3,6 +3,7 @@
<classpathentry kind="src" path="CombatTag"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="C:/Users/cheddar262/Desktop/CraftBukkit/plugins/Plugin backups/pvparena.jar"/>
<classpathentry kind="lib" path="C:/Users/cheddar262/Desktop/Plugin Development/craftbukkit-1.2.5-R3.0.jar"/>
<classpathentry kind="lib" path="C:/Users/cheddar262/Desktop/Plugin Development/Dependencies/Combat Tag/War-v1.7.1.jar"/>
<classpathentry kind="lib" path="C:/Users/cheddar262/Desktop/Plugin Development/craftbukkit-1.2.5-R4.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
153 changes: 82 additions & 71 deletions CombatTag/com/trc202/CombatTag/CombatTag.java
Expand Up @@ -26,6 +26,7 @@
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import com.tommytony.war.Warzone;
import com.topcat.npclib.NPCManager;
import com.topcat.npclib.entity.NPC;
import com.trc202.CombatTagListeners.CombatTagCommandPrevention;
Expand All @@ -38,11 +39,11 @@
import com.trc202.helpers.SettingsHelper;

public class CombatTag extends JavaPlugin {

private SettingsHelper settingsHelper;
private File settingsFile;
public Settings settings;

public final Logger log = Logger.getLogger("Minecraft");
public NPCManager npcm;
private HashMap<String,PlayerDataContainer> playerData;
Expand All @@ -52,17 +53,17 @@ public class CombatTag extends JavaPlugin {
public final NoPvpEntityListener entityListener = new NoPvpEntityListener(this);
private final NoPvpBlockListener blockListener = new NoPvpBlockListener(this);
private final CombatTagCommandPrevention commandPreventer = new CombatTagCommandPrevention(this);

private int npcNumber;

public CombatTag() {
settings = new Settings();
new File(mainDirectory).mkdirs();
settingsFile = new File(mainDirectory + File.separator + "settings.prop");
settingsHelper = new SettingsHelper(settingsFile, "CombatTag");
npcNumber = 0;
}

/**
* Change NPCManager to:
*
Expand Down Expand Up @@ -100,7 +101,7 @@ public void onEnable() {
pm.registerEvents(blockListener, this);
log.info("["+ getDescription().getName() +"]"+ " has loaded with a tag time of " + settings.getTagDuration() + " seconds");
}

/**
* Spawns an npc with the name PvPLogger at the given location, sets the npc id to be the players name
* @param plr
Expand All @@ -118,7 +119,7 @@ public NPC spawnNpc(String plr,Location location){
}
return spawnedNPC;
}

public String getNpcName(String plr) {
String npcName = settings.getNpcName();
if(!(npcName.contains("player") || npcName.contains("number")))
Expand Down Expand Up @@ -148,12 +149,12 @@ public void despawnNPC(PlayerDataContainer plrData) {
plrData.setSpawnedNPC(false);
}
}

public String getPlayerName(Entity entity){
if(npcm.isNPC(entity))return npcm.getNPCIdFromEntity(entity);
return "entity match failure";
}

/**
* Copys inventory from the Player to the NPC
* @param npc Npc
Expand All @@ -169,7 +170,7 @@ public void copyContentsNpc(NPC npc, Player plr) {
npcInv.setContents(plrInv.getContents());
}
}

public boolean hasDataContainer(String playerName){
if(playerData.containsKey(playerName)){
return playerData.containsKey(playerName);
Expand All @@ -185,8 +186,8 @@ public PlayerDataContainer getPlayerData(String playerName){
}
return playerData.get(playerName);
}


public PlayerDataContainer createPlayerData(String playerName){
PlayerDataContainer plr = new PlayerDataContainer(playerName);
playerData.put(playerName, plr);
Expand All @@ -205,7 +206,7 @@ public int getTagDuration(){
public boolean isDebugEnabled(){
return settings.isDebugEnabled();
}

/**
* Kills player and sets their inventory to an empty stack
* @param deadPlayerData
Expand All @@ -229,15 +230,15 @@ public void killPlayerEmptyInventory(PlayerDataContainer deadPlayerData) {
deadPlayerData.setPvPTimeout(0);
if (isDebugEnabled()) {log.info("[CombatTag] " + deadPlayerData.getPlayerName() + " has been killed by Combat Tag and their inventory has been emptied.");}
}

public void emptyInventory(Player target) {
PlayerInventory targetInv = target.getInventory();
targetInv.clear();
if (isDebugEnabled()) {log.info("[CombatTag] " + target.getName() + " has been killed by Combat Tag and their inventory has been emptied.");}
}



public void removeDataContainer(String playerName){
playerData.remove(playerName);
}
Expand All @@ -251,7 +252,7 @@ public int getNpcNumber() {
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
{
if(command.getName().equalsIgnoreCase("ct") || (command.getName().equalsIgnoreCase("combattag"))){
if(!args[0].equalsIgnoreCase("reload")){
if(args.length == 0){
if(sender instanceof Player){
Player player = (Player) sender;
if(hasDataContainer(player.getName()) && !getPlayerData(player.getName()).hasPVPtagExpired()){
Expand All @@ -263,12 +264,11 @@ public boolean onCommand(CommandSender sender, Command command, String commandLa
removeDataContainer(player.getName());
player.sendMessage(settings.getCommandMessageNotTagged());
}
return true;
}else{
log.info("[CombatTag] Combat Tag can only be used by a player");
return true;
log.info("[CombatTag] /ct can only be used by a player!");
}
} else{
return true;
} else if(args[0].equals("reload")){
if(sender.hasPermission("combattag.reload")){
settings = new SettingsLoader().loadSettings(settingsHelper, this.getDescription().getVersion());
if(sender instanceof Player){
Expand All @@ -279,21 +279,21 @@ public boolean onCommand(CommandSender sender, Command command, String commandLa
} else {
if(sender instanceof Player){
sender.sendMessage(ChatColor.RED + "[CombatTag] You don't have the permission 'combattag.reload'!");
} else {
log.info("[CombatTag] You don't have the permission 'combattag.reload'!");
}
}
return true;
}
sender.sendMessage(ChatColor.RED + "[CombatTag] That is not a valid command!");
return true;
}
return false;
}


public void scheduleDelayedKill(final NPC npc, final PlayerDataContainer plrData) {
long despawnTicks = settings.getNpcDespawnTime() * 20L;
final boolean kill = settings.isNpcDieAfterTime();
final Player plrNpc = (Player) npc.getBukkitEntity();
final Player plrNpc = (Player) npc.getBukkitEntity();
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
@Override
public void run() {
Expand All @@ -308,60 +308,71 @@ public void run() {
}
}, despawnTicks);
}
public boolean PvPArenaHook(Player damager, Player damaged){

public boolean PvPArenaHook(Player plr){
PVPArenaAPI pvpArenaApi = null;
boolean bothNotInArena = true;
boolean notInArena = true;
if(getServer().getPluginManager().getPlugin("pvparena") != null){
pvpArenaApi = new PVPArenaAPI();
}
if(pvpArenaApi != null)
bothNotInArena = PVPArenaAPI.getArenaName(damager) == "" && PVPArenaAPI.getArenaName(damaged) == "";
return bothNotInArena;
notInArena = PVPArenaAPI.getArenaName(plr) == "" && PVPArenaAPI.getArenaName(plr) == "";
return notInArena;
}

/**
* Loads the player data using bukkit and moves the data from the npc to the offline players file
* @param npc
* @param playerName
*/
public void updatePlayerData(NPC npc, String playerName){
Player target = this.getServer().getPlayer(playerName); //Could return the player or null
if(target == null){ //If player is offline
if(isDebugEnabled()){log.info("[CombatTag] Update player data for " + playerName + " !");}
//Create an entity to load the player data
MinecraftServer server = ((CraftServer)this.getServer()).getServer();
EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(0), playerName, new ItemInWorldManager(server.getWorldServer(0)));
target = (entity == null) ? null : (Player) entity.getBukkitEntity();
//Equivalent to
/*

public boolean WarArenaHook(Player plr){
boolean notInArena = true;
if(getServer().getPluginManager().getPlugin("War") != null){
notInArena = Warzone.getZoneByPlayerName(plr.getName()) == null && Warzone.getZoneByPlayerName(plr.getName()) == null;
}
return notInArena;
}

/**
* Loads the player data using bukkit and moves the data from the npc to the offline players file
* @param npc
* @param playerName
*/
public void updatePlayerData(NPC npc, String playerName){
Player target = this.getServer().getPlayer(playerName); //Could return the player or null
if(target == null){ //If player is offline
if(isDebugEnabled()){log.info("[CombatTag] Update player data for " + playerName + " !");}
//Create an entity to load the player data
MinecraftServer server = ((CraftServer)this.getServer()).getServer();
EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(0), playerName, new ItemInWorldManager(server.getWorldServer(0)));
target = (entity == null) ? null : (Player) entity.getBukkitEntity();
//Equivalent to
/*
if(entity == null){
target = null;
}else{
target = entity.getBukkitEntity();
}
*/
if(target != null){
target.loadData();
}
}
if(target instanceof CraftHumanEntity && npc.getBukkitEntity() instanceof CraftHumanEntity){
EntityHuman humanTarget = ((CraftHumanEntity) target).getHandle();
EntityHuman humanNpc = ((CraftHumanEntity) npc.getBukkitEntity()).getHandle();
humanTarget.copyTo(humanNpc); //Actually means copy from
if(humanNpc.getHealth() <= 0){
emptyInventory(target);
ItemStack airItem = new ItemStack(Material.AIR);
ItemStack[] emptyArmorStack = new ItemStack[4];
for(int x = 0; x < emptyArmorStack.length; x++){
emptyArmorStack[x] = airItem;
}
target.getInventory().setArmorContents(emptyArmorStack);
}
} else {
log.info("[" + this.getDescription().getName() + "] Something went wrong!");
log.info("[" + this.getDescription().getName() + "] The target or source of copyTo is not a Human Entity");
}
target.saveData();
}
*/
if(target != null){
target.loadData();
}
}
if(target instanceof CraftHumanEntity && npc.getBukkitEntity() instanceof CraftHumanEntity){
EntityHuman humanTarget = ((CraftHumanEntity) target).getHandle();
EntityHuman humanNpc = ((CraftHumanEntity) npc.getBukkitEntity()).getHandle();
humanTarget.copyTo(humanNpc); //Actually means copy from
if(humanNpc.getHealth() <= 0){
emptyInventory(target);
ItemStack airItem = new ItemStack(Material.AIR);
ItemStack[] emptyArmorStack = new ItemStack[4];
for(int x = 0; x < emptyArmorStack.length; x++){
emptyArmorStack[x] = airItem;
}
target.getInventory().setArmorContents(emptyArmorStack);
humanTarget.setHealth(0);
PlayerDataContainer playerData = getPlayerData(playerName);
playerData.setPvPTimeout(0);
}
} else {
log.info("[" + this.getDescription().getName() + "] Something went wrong!");
log.info("[" + this.getDescription().getName() + "] The target or source of copyTo is not a Human Entity");
}
target.saveData();
}
}
Expand Up @@ -17,7 +17,7 @@ public CombatTagCommandPrevention(CombatTag plugin){
this.plugin = plugin;
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
{
if(event.isCancelled()) return;
Expand Down
Expand Up @@ -85,7 +85,7 @@ private void onPlayerDamageByPlayerNPCMode(Player damager, Player damaged){
if(plugin.npcm.isNPC(damaged)){return;} //If the damaged player is an npc do nothing
PlayerDataContainer damagerData;
PlayerDataContainer damagedData;
boolean bothNotInArena = plugin.PvPArenaHook(damager, damaged);
boolean bothNotInArena = plugin.PvPArenaHook(damager) && plugin.PvPArenaHook(damaged) && plugin.WarArenaHook(damager) && plugin.WarArenaHook(damaged);
if(bothNotInArena){
if(!damager.hasPermission("combattag.ignore")){
//Get damager player data container
Expand Down
Expand Up @@ -116,12 +116,12 @@ private void onPlayerJoin(Player loginPlayer){
assert(loginPlayer.getHealth() == loginDataContainer.getHealth());
loginPlayer.setLastDamageCause(new EntityDamageEvent(loginPlayer, DamageCause.ENTITY_EXPLOSION, 0));
loginPlayer.setNoDamageTicks(0);
loginDataContainer.setShouldBePunished(false);
}
plugin.removeDataContainer(loginPlayer.getName());
plugin.createPlayerData(loginPlayer.getName()).setPvPTimeout(plugin.getTagDuration());
loginDataContainer = plugin.getPlayerData(loginPlayer.getName());
if(loginPlayer.getHealth() > 0){
loginDataContainer.setPvPTimeout(plugin.getTagDuration());
}
loginDataContainer.setShouldBePunished(false);
loginDataContainer.setSpawnedNPC(false);
}
}

Expand Down

0 comments on commit bd97e4f

Please sign in to comment.