more entity events and entity item api

This commit is contained in:
gnembon committed 2019-09-03 09:16:18 -03:00
1 parent 25b37913f2
commit 06b3f7c532
9 files changed
+121 -21

No files matched your search

@@ -4,6 +4,7 @@ import carpet.script.CarpetContext;
import carpet.script.CarpetEventServer; import carpet.script.CarpetEventServer;
import carpet.script.ScriptHost; import carpet.script.ScriptHost;
import carpet.script.value.Value; import carpet.script.value.Value;
import net.minecraft.entity.damage.DamageSource;
import java.util.List; import java.util.List;
@@ -13,8 +14,8 @@ public interface EntityInterface
//scarpet part //scarpet part
void onDeathCallback(String reason); void onDeathCallback(String reason);
void setDeathCallback(CarpetContext cc, String function, List<Value> extraArgs); void setDeathCallback(CarpetContext cc, String function, List<Value> extraArgs);
void onDamageCallback(); void onDamageCallback(float amount, DamageSource source);
void setDamageCallback(CarpetContext cc, String function, List<Value> extraArgs); void setDamageCallback(CarpetContext cc, String function, List<Value> extraArgs);
void onRemovedCallback();
void setRemovedCallback(CarpetContext cc, String function, List<Value> extraArgs); void setRemovedCallback(CarpetContext cc, String function, List<Value> extraArgs);
void setTickCallback(CarpetContext cc, String function, List<Value> extraArgs);
} }
@@ -0,0 +1,7 @@
package carpet.fakes;
public interface ItemEntityInterface
{
int getAge();
int getPickupDelay();
}
@@ -0,0 +1,24 @@
package carpet.mixins;
import carpet.fakes.EntityInterface;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageTracker;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(DamageTracker.class)
public class DamageTracker_scarpetEventMixin
{
@Shadow @Final private LivingEntity entity;
@Inject(method = "onDamage", at = @At("HEAD"))
private void onDamageTaken(DamageSource damageSource_1, float float_1, float float_2, CallbackInfo ci)
{
((EntityInterface)entity).onDamageCallback(float_2, damageSource_1);
}
}
@@ -8,6 +8,7 @@ import carpet.script.ScriptHost;
import carpet.script.value.Value; import carpet.script.value.Value;
import carpet.settings.CarpetSettings; import carpet.settings.CarpetSettings;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import org.lwjgl.system.CallbackI; import org.lwjgl.system.CallbackI;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
@@ -23,16 +24,11 @@ public abstract class Entity_scarpetEventsMixin implements EntityInterface
@Shadow public boolean removed; @Shadow public boolean removed;
private CarpetEventServer.ScheduledCall deathCall; private CarpetEventServer.ScheduledCall deathCall;
private CarpetEventServer.ScheduledCall removeCall; private CarpetEventServer.ScheduledCall removeCall;
private CarpetEventServer.ScheduledCall tickCall;
private CarpetEventServer.ScheduledCall damageCall;
@Override
public void onDeathCallback(String reason)
{
if (deathCall != null)
{
CarpetServer.scriptServer.events.onEntityDeath(deathCall, (Entity)(Object)this, reason);
}
}
@Override @Override
public void setDeathCallback(CarpetContext cc, String function, List<Value> extraArgs) public void setDeathCallback(CarpetContext cc, String function, List<Value> extraArgs)
@@ -46,6 +42,47 @@ public abstract class Entity_scarpetEventsMixin implements EntityInterface
removeCall = CarpetServer.scriptServer.events.makeRemovedCall(cc, function, extraArgs); removeCall = CarpetServer.scriptServer.events.makeRemovedCall(cc, function, extraArgs);
} }
@Override
public void setTickCallback(CarpetContext cc, String function, List<Value> extraArgs)
{
tickCall = CarpetServer.scriptServer.events.makeTickCall(cc, function, extraArgs);
}
@Override
public void setDamageCallback(CarpetContext cc, String function, List<Value> extraArgs)
{
damageCall = CarpetServer.scriptServer.events.makeDamageCall(cc, function, extraArgs);
}
@Override
public void onDeathCallback(String reason)
{
if (deathCall != null)
{
CarpetServer.scriptServer.events.onEntityDeath(deathCall, (Entity)(Object)this, reason);
}
}
@Override
public void onDamageCallback(float amount, DamageSource source)
{
if (damageCall != null)
{
CarpetServer.scriptServer.events.onEntityDamage(damageCall, (Entity)(Object)this, amount, source);
}
}
@Inject(method = "tick", at = @At("HEAD"))
private void onTickCall(CallbackInfo ci)
{
if (tickCall != null)
{
CarpetServer.scriptServer.events.onEntityTick(tickCall, (Entity) (Object) this);
}
}
@Inject(method = "remove", at = @At("HEAD")) @Inject(method = "remove", at = @At("HEAD"))
private void onRemove(CallbackInfo ci) private void onRemove(CallbackInfo ci)
@@ -2,7 +2,7 @@ package carpet.mixins;
import carpet.settings.CarpetSettings; import carpet.settings.CarpetSettings;
import carpet.helpers.InventoryHelper; import carpet.helpers.InventoryHelper;
import carpet.utils.IItemEntity; import carpet.fakes.ItemEntityInterface;
import net.minecraft.block.ShulkerBoxBlock; import net.minecraft.block.ShulkerBoxBlock;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
@@ -18,7 +18,8 @@ import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ItemEntity.class) @Mixin(ItemEntity.class)
public abstract class ItemEntityMixin extends Entity implements IItemEntity { public abstract class ItemEntityMixin extends Entity implements ItemEntityInterface
{
private static final int SHULKERBOX_MAX_STACK_AMOUNT = 64; private static final int SHULKERBOX_MAX_STACK_AMOUNT = 64;
@Shadow private int age; @Shadow private int age;
@@ -89,8 +90,8 @@ public abstract class ItemEntityMixin extends Entity implements IItemEntity {
selfStack.increment(amount); selfStack.increment(amount);
self.setStack(selfStack); self.setStack(selfStack);
this.pickupDelay = Math.max(((IItemEntity)other).getPickupDelay(), this.pickupDelay); this.pickupDelay = Math.max(((ItemEntityInterface)other).getPickupDelay(), this.pickupDelay);
this.age = Math.min(((IItemEntity)other).getAge(), this.age); this.age = Math.min(((ItemEntityInterface)other).getAge(), this.age);
otherStack.decrement(amount); otherStack.decrement(amount);
if (otherStack.isEmpty()) if (otherStack.isEmpty())
@@ -9,6 +9,7 @@ import carpet.script.value.StringValue;
import carpet.script.value.Value; import carpet.script.value.Value;
import carpet.settings.CarpetSettings; import carpet.settings.CarpetSettings;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@@ -390,6 +391,15 @@ public class CarpetEventServer
{ {
return makeEventCall(cc, function, extraArgs, 1); return makeEventCall(cc, function, extraArgs, 1);
} }
public ScheduledCall makeTickCall(CarpetContext cc, String function, List<Value> extraArgs)
{
return makeEventCall(cc, function, extraArgs, 1);
}
public ScheduledCall makeDamageCall(CarpetContext cc, String function, List<Value> extraArgs)
{
return makeEventCall(cc, function, extraArgs, 3);
}
public void onEntityDeath(ScheduledCall call, Entity e, String reason) public void onEntityDeath(ScheduledCall call, Entity e, String reason)
@@ -400,5 +410,20 @@ public class CarpetEventServer
{ {
removeCall.execute(Collections.singletonList((c, t) -> new EntityValue(entity))); removeCall.execute(Collections.singletonList((c, t) -> new EntityValue(entity)));
} }
public void onEntityTick(ScheduledCall tickCall, Entity entity)
{
tickCall.execute(Collections.singletonList((c, t) -> new EntityValue(entity)));
}
public void onEntityDamage(ScheduledCall call, Entity e, float amount, DamageSource source)
{
call.execute(Arrays.asList(
(c, t) -> new EntityValue(e),
(c, t) -> new NumericValue(amount),
(c, t) -> new StringValue(source.getName()),
(c, t) -> source.getAttacker()==null?Value.NULL:new EntityValue(source.getAttacker())
));
}
} }
@@ -1,6 +1,7 @@
package carpet.script.value; package carpet.script.value;
import carpet.fakes.EntityInterface; import carpet.fakes.EntityInterface;
import carpet.fakes.ItemEntityInterface;
import carpet.fakes.MobEntityInterface; import carpet.fakes.MobEntityInterface;
import carpet.helpers.Tracer; import carpet.helpers.Tracer;
import carpet.script.CarpetContext; import carpet.script.CarpetContext;
@@ -216,8 +217,8 @@ public class EntityValue extends Value
put("age", (e, a) -> new NumericValue(e.age)); put("age", (e, a) -> new NumericValue(e.age));
put("item", (e, a) -> (e instanceof ItemEntity)?new StringValue(((ItemEntity) e).getStack().getName().getString()):Value.NULL); put("item", (e, a) -> (e instanceof ItemEntity)?new StringValue(((ItemEntity) e).getStack().getName().getString()):Value.NULL);
put("count", (e, a) -> (e instanceof ItemEntity)?new NumericValue(((ItemEntity) e).getStack().getCount()):Value.NULL); put("count", (e, a) -> (e instanceof ItemEntity)?new NumericValue(((ItemEntity) e).getStack().getCount()):Value.NULL);
put("pickup_delay", (e, a) -> (e instanceof ItemEntity)?new NumericValue(((ItemEntityInterface) e).getPickupDelay()):Value.NULL);
// ItemEntity -> despawn timer via ssGetAge // ItemEntity -> despawn timer via ssGetAge
// TODO item pickup delay read and write
put("is_baby", (e, a) -> (e instanceof LivingEntity)?new NumericValue(((LivingEntity) e).isBaby()):Value.NULL); put("is_baby", (e, a) -> (e instanceof LivingEntity)?new NumericValue(((LivingEntity) e).isBaby()):Value.NULL);
put("target", (e, a) -> { put("target", (e, a) -> {
if (e instanceof MobEntity) if (e instanceof MobEntity)
@@ -692,6 +693,13 @@ public class EntityValue extends Value
} }
}); //requires mixing }); //requires mixing
put("pickup_delay", (e, v) ->
{
if (e instanceof ItemEntity)
{
((ItemEntity) e).setPickupDelay((int)NumericValue.asNumber(v).getLong());
}
});
// gamemode // gamemode
// spectate // spectate
@@ -721,5 +729,7 @@ public class EntityValue extends Value
private static Map<String, Fluff.QuadConsumer<CarpetContext, Entity, String, List<Value>>> events = new HashMap<String, Fluff.QuadConsumer<CarpetContext, Entity, String, List<Value>>>() {{ private static Map<String, Fluff.QuadConsumer<CarpetContext, Entity, String, List<Value>>> events = new HashMap<String, Fluff.QuadConsumer<CarpetContext, Entity, String, List<Value>>>() {{
put("on_death", (c, e, f, l) -> ((EntityInterface)e).setDeathCallback(c, f, l)); put("on_death", (c, e, f, l) -> ((EntityInterface)e).setDeathCallback(c, f, l));
put("on_removed", (c, e, f, l) -> ((EntityInterface)e).setRemovedCallback(c, f, l)); put("on_removed", (c, e, f, l) -> ((EntityInterface)e).setRemovedCallback(c, f, l));
put("on_tick", (c, e, f, l) -> ((EntityInterface)e).setTickCallback(c, f, l));
put("on_damage", (c, e, f, l) -> ((EntityInterface)e).setDamageCallback(c, f, l));
}}; }};
} }
@@ -1,6 +0,0 @@
package carpet.utils;
public interface IItemEntity {
int getAge();
int getPickupDelay();
}
+1
View File
@@ -119,6 +119,7 @@
"Entity_scarpetEventsMixin", "Entity_scarpetEventsMixin",
"LivingEntity_scarpetEventsMixin", "LivingEntity_scarpetEventsMixin",
"DamageTracker_scarpetEventMixin",
"PistonBlock_movableTEMixin", "PistonBlock_movableTEMixin",