Skip to content

Commit

Permalink
Fix errors in OfflinePlayerLoader
Browse files Browse the repository at this point in the history
Fix errors in updatePlayerData
Finalise to version 6.4.0
  • Loading branch information
Techcable committed Mar 24, 2015
1 parent f3f44ab commit 60cfc30
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 230 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.trc202</groupId>
<artifactId>combattag</artifactId>
<version>6.4.0-SNAPSHOT</version>
<version>6.4.0</version>
<name>CombatTag</name>
<properties>
<java.version>1.6</java.version>
Expand Down
45 changes: 27 additions & 18 deletions src/main/java/com/trc202/CombatTag/CombatTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -18,6 +19,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
Expand All @@ -37,6 +39,8 @@
import com.trc202.settings.SettingsHelper;
import com.trc202.settings.SettingsLoader;

import static com.trc202.CombatTag.Reflection.*;

public class CombatTag extends JavaPlugin {

private final SettingsHelper settingsHelper;
Expand Down Expand Up @@ -87,11 +91,13 @@ public CombatTag() {
*/
@Override
public void onDisable() {
for (NPC npc : npcm.getNPCs()) {
UUID uuid = npcm.getNPCIdFromEntity(npc.getEntity());
despawnNPC(uuid, NpcDespawnReason.PLUGIN_DISABLED);
if (isDebugEnabled()) {
log.info("[CombatTag] Disabling npc with ID of: " + uuid);
if (npcm != null) {
for (NPC npc : npcm.getNPCs()) {
UUID uuid = npcm.getNPCIdFromEntity(npc.getEntity());
despawnNPC(uuid, NpcDespawnReason.PLUGIN_DISABLED);
if (isDebugEnabled()) {
log.info("[CombatTag] Disabling npc with ID of: " + uuid);
}
}
}
//Just in case...
Expand Down Expand Up @@ -367,7 +373,7 @@ public boolean onCommand(CommandSender sender, Command command, String commandLa
sender.sendMessage("Please specify a player to force into combat");
return true;
}
if (isInCombat(toForce.getUniqueId())) {
if (!isInCombat(toForce.getUniqueId())) {
tagged.put(toForce.getUniqueId(), PvPTimeout(60));
if (!toForce.equals(sender)) sender.sendMessage("You have been forced into combat for one minute");
sender.sendMessage("Sucessfuly forced " + toForce.getName() + " into combat.");
Expand Down Expand Up @@ -457,7 +463,7 @@ public void updatePlayerData(NPC npc, UUID playerUUID) {
emptyArmorStack[x] = airItem;
}
target.getInventory().setArmorContents(emptyArmorStack);
target.setHealth(0);
setHealth(target, healthCheck(source.getHealth()));
} else {
copyTo(target, source);
}
Expand All @@ -480,13 +486,7 @@ public void copyTo(Player target, Player source) {
target.setExhaustion(source.getExhaustion());
target.setSaturation(source.getSaturation());
target.setFireTicks(source.getFireTicks());
if (target instanceof HumanEntity) {
HumanEntity humanTarget = (HumanEntity) target;
double healthSet = healthCheck(source.getHealth());
humanTarget.setHealth((float) healthSet);
} else {
log.info("[CombatTag] An error has occurred! Target is not a HumanEntity!");
}
setHealth(target, healthCheck(source.getHealth()));
}

public double healthCheck(double health) {
Expand All @@ -506,13 +506,22 @@ public SettingsHelper getSettingsHelper() {
public static boolean isVersionSupported() {
return NPCLib.isSupported();
}

public static final Field ENTITY_PLAYER_INVULNERABLE_TICKS_FIELD = Reflection.makeField(Reflection.getNmsClass("EntityPlayer"), "invulnerableTicks");


public static final Field ENTITY_PLAYER_INVULNERABLE_TICKS_FIELD = Reflection.makeField(getNmsClass("EntityPlayer"), "invulnerableTicks");
public static final Method ENTITY_LIVING_SET_HEALTH_METHOD = Reflection.makeMethod(getNmsClass("EntityLiving"), "setHealth", float.class);

public static void setInvulnerableTicks(Entity bukkitEntity, int invulnerableTicks) { //Entity.setNoDamageTicks() doesn't set EntityPlayer.invulnerableTicks
Object entity = Reflection.getHandle(bukkitEntity);
if (Reflection.getNmsClass("EntityPlayer").isInstance(entity)) {
if (getNmsClass("EntityPlayer").isInstance(entity)) {
Reflection.setField(ENTITY_PLAYER_INVULNERABLE_TICKS_FIELD, entity, invulnerableTicks);
}
}
/*
* EntityLiving.setHealth() calls die() if the entity is a player.
* EntityPlayer.die() tries to close any open inventories, causing the offline player to throw an exception.
*/
public static void setHealth(LivingEntity bukkitEntity, double health) {
Object entity = Reflection.getHandle(bukkitEntity);
Reflection.callMethod(ENTITY_LIVING_SET_HEALTH_METHOD, entity, (float)health);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/trc202/CombatTag/NPCManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public class NPCManager {
public NPCManager(CombatTag plugin) {
this.plugin = plugin;
getRegistry();
}

private final CombatTag plugin;
Expand Down
Loading

0 comments on commit 60cfc30

Please sign in to comment.