Skip to content

Commit

Permalink
Major refactor to better handle PVP being on. Reduces listener quanti…
Browse files Browse the repository at this point in the history
…ty to one.
  • Loading branch information
kolinkrewinkel committed Sep 23, 2013
1 parent 12b6e9c commit b8e0e5f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 44 deletions.
56 changes: 32 additions & 24 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 11 additions & 19 deletions src/main/java/com/bitlimit/NPCs/BlacksmithInteractBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,37 @@
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.block.Block;
import org.bukkit.*;

public class BlacksmithInteractBehavior extends InteractBehavior implements Listener {
private Plugin plugin;

public class BlacksmithInteractBehavior extends InteractBehavior {
private static Material defaultItem = Material.DIAMOND_AXE;
private final Plugin plugin;

public BlacksmithInteractBehavior(RemoteEntity inEntity) {
super(inEntity);

this.plugin = inEntity.getManager().getPlugin();
this.plugin.getServer().getPluginManager().registerEvents(this, this.plugin);

this.onEntityUpdate();
}

public void onInteract(Player player) {

}

public void onEntityUpdate()
{
if (this.m_entity.getBukkitEntity() == null)
return;

this.m_entity.setPushable(false);
this.m_entity.setStationary(false);

Player npc = (Player)this.m_entity.getBukkitEntity();
npc.setCanPickupItems(false);
Expand All @@ -59,27 +64,14 @@ public void onEntityUpdate()

npc.getInventory().setArmorContents(armor); // Set it.

this.m_entity.setStationary(false, false);
}

public void onInteract(Player inPlayer) {
((RemotePlayer)this.getRemoteEntity()).fakeDamage();
((RemotePlayer)this.getRemoteEntity()).doArmSwing() ;
}

@EventHandler
public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
Player inPlayer = event.getPlayer();
public void onRightClickInteract(Player inPlayer) {
RemotePlayer behaviorEntity = (RemotePlayer) this.getRemoteEntity();

if (!event.getRightClicked().equals(behaviorEntity.getBukkitEntity())) {
return;
}

Player npc = (Player) behaviorEntity.getBukkitEntity();

ItemStack actionItem = inPlayer.getItemInHand();

if (actionItem.getType() == Material.NAME_TAG) {
if (actionItem.getItemMeta().hasDisplayName()) {

Expand Down
50 changes: 49 additions & 1 deletion src/main/java/com/bitlimit/NPCs/NPCs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

import de.kumpelblase2.remoteentities.EntityManager;
import de.kumpelblase2.remoteentities.RemoteEntities;
import de.kumpelblase2.remoteentities.api.RemoteEntity;
import de.kumpelblase2.remoteentities.api.thinking.InteractBehavior;
import de.kumpelblase2.remoteentities.entities.RemotePlayer;
import de.kumpelblase2.remoteentities.persistence.serializers.JSONSerializer;
import de.kumpelblase2.remoteentities.persistence.serializers.YMLSerializer;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class NPCs extends JavaPlugin {
public class NPCs extends JavaPlugin implements Listener {
public EntityManager manager;
public NPCCommandExecutor commandExecutor;

Expand Down Expand Up @@ -46,6 +54,8 @@ public void onEnable() {

this.commandExecutor = new NPCCommandExecutor(this);
this.getCommand("npc").setExecutor(this.commandExecutor);

this.getServer().getPluginManager().registerEvents(this, this);
}

@Override
Expand All @@ -66,4 +76,42 @@ public void saveData() {

}

@EventHandler
public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {

if (!(event.getEntity() instanceof LivingEntity)) {
return;
}

LivingEntity livingEntity = (LivingEntity)event.getEntity();

if (RemoteEntities.isRemoteEntity(livingEntity)) {
event.setCancelled(true);

RemoteEntity remoteEntity = RemoteEntities.getRemoteEntityFromEntity(livingEntity);
if (remoteEntity instanceof RemotePlayer) {
RemotePlayer remotePlayer = (RemotePlayer)remoteEntity;
remotePlayer.fakeDamage();
remotePlayer.doArmSwing();
}
}
}

@EventHandler
public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
if (!(event.getRightClicked() instanceof LivingEntity)) {
return;
}

LivingEntity livingEntity = (LivingEntity)event.getRightClicked();
if (RemoteEntities.isRemoteEntity(livingEntity)) {
RemoteEntity remoteEntity = RemoteEntities.getRemoteEntityFromEntity(livingEntity);

InteractBehavior interactBehavior = (InteractBehavior)remoteEntity.getMind().getBehaviour("Interact");
if (interactBehavior instanceof BlacksmithInteractBehavior) {
((BlacksmithInteractBehavior)interactBehavior).onRightClickInteract(event.getPlayer());
}
}
}

}

0 comments on commit b8e0e5f

Please sign in to comment.