Skip to content

Commit

Permalink
player.pickupitem hook now accepts a RESULT return value
Browse files Browse the repository at this point in the history
Added entity.dropall hook (entity, table, lootLVL, hitRecently)
Added player.bonemeal hook (player, world, block)
Added player.destroyitem hook (player, itemstack)
Changed player.dropall hook to have same aguments as entity.dropall
Added player.placeblock and player.rightclick hooks. Arguments reflect player.mineblock
  • Loading branch information
bkacjios committed Jan 27, 2016
1 parent 9d162ce commit 2b78f1a
Showing 1 changed file with 139 additions and 21 deletions.
160 changes: 139 additions & 21 deletions main/java/com/luacraft/LuaEventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.event.entity.player.BonemealEvent;
import net.minecraftforge.event.entity.player.EntityInteractEvent;
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
import net.minecraftforge.event.entity.player.PlayerDropsEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.PlayerOpenContainerEvent;
Expand Down Expand Up @@ -182,7 +185,7 @@ public void onItemCrafted(ItemCraftedEvent event) {
* @function player.pickupitem
* @info Called whenever a player picks up an item
* @arguments [[Player]]:player, [[EntityItem]]:item
* @return nil
* @return [[RESULT]]:result
*/

@SubscribeEvent
Expand All @@ -196,9 +199,15 @@ public void onItemPickup(ItemPickupEvent event) {
l.pushString("player.pickupitem");
LuaUserdataManager.PushUserdata(l, event.player);
LuaUserdataManager.PushUserdata(l, event.pickedUp);
l.call(3, 0);
l.call(3, 1);

if (!l.isNil(-1))
event.setResult(Result.values()[l.checkInteger(-1, Result.DEFAULT.ordinal())]);

} catch (Exception e) {
l.handleException(e);
} finally {
l.setTop(0);
}
}
}
Expand Down Expand Up @@ -699,7 +708,45 @@ public void onLivingDeath(LivingDeathEvent event) {
}
}

// TODO: LivingDropsEvent
/**
* @author Jake
* @function entity.dropall
* @info Called when a entity dies and drops all their loot
* @arguments [[Entity]]:entity, [[Table]]:drops, [[Number]]:lootLevel, [[Boolean]]:hitRecent
* @return [[Boolean]]:cancel
*/

@SubscribeEvent
public void onEntityDrops(LivingDropsEvent event) {
synchronized (l) {
if (!l.isOpen())
return;

try {
l.pushHookCall();
l.pushString("entity.dropall");
LuaUserdataManager.PushUserdata(l, event.entity);
l.pushUserdataWithMeta(event.source, "DamageSource");
l.newTable();
for (int i = 0; i < event.drops.size(); i++) {
l.pushNumber(i + 1);
LuaUserdataManager.PushUserdata(l, event.drops.get(i));
l.setTable(-3);
}
l.pushNumber(event.lootingLevel);
l.pushBoolean(event.recentlyHit);
l.call(6, 1);

if (!l.isNil(-1))
event.setCanceled(l.toBoolean(-1));

} catch (Exception e) {
l.handleException(e);
} finally {
l.setTop(0);
}
}
}

/**
* @author Jake
Expand Down Expand Up @@ -791,7 +838,32 @@ public void onLivingUpdate(LivingUpdateEvent event) {

// TODO: AttackEntityEvent (covered by living?)

// TODO: BonemealEvent
@SubscribeEvent
public void onPlayerBonemeal(BonemealEvent event) {
synchronized (l) {
if (!l.isOpen())
return;

try {
l.pushHookCall();
l.pushString("player.bonemeal");
LuaUserdataManager.PushUserdata(l, event.entityPlayer);
LuaUserdataManager.PushUserdata(l, event.world);

LuaJavaBlock thisBlock = new LuaJavaBlock(event.entityPlayer.worldObj, event.pos);
LuaUserdataManager.PushUserdata(l, thisBlock);
l.call(4, 1);

if (!l.isNil(-1))
event.setResult(Result.values()[l.checkInteger(-1, Result.DEFAULT.ordinal())]);

} catch (Exception e) {
l.handleException(e);
} finally {
l.setTop(0);
}
}
}

// TODO: BreakSpeed

Expand Down Expand Up @@ -835,21 +907,41 @@ public void onEntityInteract(EntityInteractEvent event) {

// TODO: ItemTooltipEvent

// TODO: PlayerDestroyItemEvent
/**
* @author Jake
* @function player.destroyitem
* @info Called when a player destroys an item in their inventory, normally when completing an action
* @arguments [[Player]]:player, [[ItemStack]]:original
* @return [[Boolean]]:cancel
*/

public void onPlayerDestroyItem(PlayerDestroyItemEvent event) {
synchronized (l) {
if (!l.isOpen())
return;

try {
l.pushHookCall();
l.pushString("player.destroyitem");
LuaUserdataManager.PushUserdata(l, event.entityPlayer);
l.pushUserdataWithMeta(event.original, "ItemStack");
l.call(3, 1);
} catch (Exception e) {
l.handleException(e);
}
}
}

/**
* @author Jake
* @function player.dropall
* @info Called when a player dies and drops all their loot
* @arguments [[Player]]:player
* @arguments [[Player]]:player, [[Table]]:drops, [[Number]]:lootLevel, [[Boolean]]:hitRecent
* @return [[Boolean]]:cancel
*/

@SubscribeEvent
public void onPlayerDrops(PlayerDropsEvent event) {
// Child class of LivingDropsEvent that is fired specifically when a
// player dies. Canceling the event will prevent ALL drops from entering
// the world.
synchronized (l) {
if (!l.isOpen())
return;
Expand All @@ -858,7 +950,16 @@ public void onPlayerDrops(PlayerDropsEvent event) {
l.pushHookCall();
l.pushString("player.dropall");
LuaUserdataManager.PushUserdata(l, event.entityPlayer);
l.call(2, 1);
l.pushUserdataWithMeta(event.source, "DamageSource");
l.newTable();
for (int i = 0; i < event.drops.size(); i++) {
l.pushNumber(i + 1);
LuaUserdataManager.PushUserdata(l, event.drops.get(i));
l.setTable(-3);
}
l.pushNumber(event.lootingLevel);
l.pushBoolean(event.recentlyHit);
l.call(6, 1);

if (!l.isNil(-1))
event.setCanceled(l.toBoolean(-1));
Expand All @@ -881,22 +982,44 @@ public void onPlayerDrops(PlayerDropsEvent event) {
* @return [[Boolean]]:cancel
*/

// TODO: player.placeblock
/**
* @author Jake
* @function player.rightclick
* @info Called when a player presses right mouse on nothing
* @arguments [[Player]]:player, [[Block]]:block, [[Vector]]:normal
* @return [[Boolean]]:cancel
*/

/**
* @author Jake
* @function player.placeblock
* @info Called when a player attempts to place a block
* @arguments [[Player]]:player, [[Block]]:block, [[Vector]]:normal
* @return [[Boolean]]:cancel
*/

@SubscribeEvent
public void onPlayerInteract(PlayerInteractEvent event) {
synchronized (l) {
if (!l.isOpen())
return;

if (event.action != PlayerInteractEvent.Action.LEFT_CLICK_BLOCK)
return;

try {
l.pushHookCall();
l.pushString("player.mineblock");
LuaUserdataManager.PushUserdata(l, event.entityPlayer);

switch (event.action) {
case LEFT_CLICK_BLOCK:
l.pushString("player.mineblock");
break;
case RIGHT_CLICK_AIR:
l.pushString("player.rightclick");
break;
case RIGHT_CLICK_BLOCK:
l.pushString("player.placeblock");
break;
}

LuaUserdataManager.PushUserdata(l, event.entityPlayer);
LuaJavaBlock thisBlock = new LuaJavaBlock(event.entityPlayer.worldObj, event.pos);
LuaUserdataManager.PushUserdata(l, thisBlock);
l.pushFace(event.face);
Expand Down Expand Up @@ -945,11 +1068,6 @@ public void onPlayerOpenContainer(PlayerOpenContainerEvent event) {
}
}

// TODO: PlayerPickupXpEvent
// This event is called when a player collides with a EntityXPOrb on the
// ground. The event can be canceled, and no further processing will be
// done.

/**
* @author Jake
* @function player.pickupxp
Expand Down

0 comments on commit 2b78f1a

Please sign in to comment.