statistics, block placed and interacted events, stats event test

This commit is contained in:
gnembon committed 2019-12-27 03:25:56 -04:00
1 parent 860c9b6d21
commit d7ca974f1a
12 files changed
+210 -7

No files matched your search

@@ -296,6 +296,43 @@ public class CarpetEventServer
}, player::getCommandSource);
}
},
PLAYER_INTERACTS_WITH_BLOCK("player_interacts_with_block", new CallbackList(5))
{
@Override
public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes)
{
handler.call( () ->
{
BlockPos blockpos = hitRes.getBlockPos();
Direction enumfacing = hitRes.getSide();
Vec3d vec3d = hitRes.getPos().subtract(blockpos.getX(), blockpos.getY(), blockpos.getZ());
return Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
((c, t) -> new BlockValue(null, player.getServerWorld(), blockpos)),
((c, t) -> new StringValue(enumfacing.getName())),
((c, t) -> ListValue.of(
new NumericValue(vec3d.x),
new NumericValue(vec3d.y),
new NumericValue(vec3d.z)
))
);
}, player::getCommandSource);
}
},
PLAYER_PLACES_BLOCK("player_places_block", new CallbackList(4))
{
@Override
public void onBlockPlaced(ServerPlayerEntity player, BlockPos pos, Hand enumhand, ItemStack itemstack)
{
handler.call( () -> Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> ListValue.fromItemStack(itemstack)),
((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")),
((c, t) -> new BlockValue(null, player.getServerWorld(), pos))
), player::getCommandSource);
}
},
PLAYER_BREAK_BLOCK("player_breaks_block",new CallbackList(2))
{
@Override
@@ -307,7 +344,7 @@ public class CarpetEventServer
), player::getCommandSource);
}
},
PLAYER_INERACTSW_WITH_ENTITY("player_interacts_with_entity",new CallbackList(3))
PLAYER_INTERACTS_WITH_ENTITY("player_interacts_with_entity",new CallbackList(3))
{
@Override
public void onEntityAction(ServerPlayerEntity player, Entity entity, Hand enumhand)
@@ -465,6 +502,7 @@ public class CarpetEventServer
public void onBlockAction(ServerPlayerEntity player, BlockPos blockpos, Direction facing) { }
public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes) { }
public void onBlockBroken(ServerPlayerEntity player, BlockPos pos, BlockState previousBS) { }
public void onBlockPlaced(ServerPlayerEntity player, BlockPos pos, Hand enumhand, ItemStack itemstack) { }
public void onEntityAction(ServerPlayerEntity player, Entity entity, Hand enumhand) { }
}
@@ -2,6 +2,7 @@ package carpet.script;
import carpet.CarpetServer;
import carpet.fakes.MinecraftServerInterface;
import carpet.fakes.StatTypeInterface;
import carpet.helpers.FeatureGenerator;
import carpet.mixins.ChunkTicketManager_scarpetMixin;
import carpet.mixins.ServerChunkManager_scarpetMixin;
@@ -68,11 +69,14 @@ import net.minecraft.server.world.ChunkTicket;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.sound.SoundCategory;
import net.minecraft.stat.Stat;
import net.minecraft.stat.StatType;
import net.minecraft.state.StateFactory;
import net.minecraft.state.property.Property;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Clearable;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.ChunkPos;
@@ -3217,8 +3221,43 @@ public class CarpetExpression
((CarpetScriptHost)((CarpetContext)c).host).setGlobalState(tag, file);
return (cc, tt) -> Value.NULL;
});
this.expr.addLazyFunction("statistic", 3, (c, t, lv) ->
{
Value playerValue = lv.get(0).evalValue(c);
CarpetContext cc = (CarpetContext)c;
ServerPlayerEntity player = EntityValue.getPlayerByValue(cc.s.getMinecraftServer(), playerValue);
if (player == null) return LazyValue.NULL;
Identifier category;
Identifier statName;
try
{
category = new Identifier(lv.get(1).evalValue(c).getString());
statName = new Identifier(lv.get(2).evalValue(c).getString());
}
catch (InvalidIdentifierException e)
{
return LazyValue.NULL;
}
StatType<?> type = Registry.STAT_TYPE.get(category);
if (type == null)
return LazyValue.NULL;
Stat<?> stat = getStat(type, statName);
if (stat == null)
return LazyValue.NULL;
return (_c, _t) -> new NumericValue(player.getStatHandler().getStat(stat));
});
}
private <T> Stat<T> getStat(StatType<T> type, Identifier id)
{
T key = type.getRegistry().get(id);
if (key == null || !((StatTypeInterface)type).hasStatCreated(key))
return null;
return type.getOrCreateStat(key);
}
/**
* <h1>.</h1>
* @param expression expression
@@ -46,6 +46,7 @@ public class CarpetScriptServer
private static List<ModuleInterface> bundledModuleData = new ArrayList<ModuleInterface>(){{
add(new BundledModule("camera"));
add(new BundledModule("event_test"));
add(new BundledModule("stats_test"));
}};
public CarpetScriptServer()
@@ -166,6 +167,11 @@ public class CarpetScriptServer
//TODO add per player modules to support player actions better on a server
name = name.toLowerCase(Locale.ROOT);
ModuleInterface module = getModule(name);
if (module == null)
{
Messenger.m(source, "r Failed to add "+name+" app");
return false;
}
CarpetScriptHost newHost = createMinecraftScriptHost(name, module, perPlayer, source);
if (newHost == null)
{
@@ -29,6 +29,7 @@ import net.minecraft.entity.mob.MobEntityWithAi;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
@@ -93,6 +94,25 @@ public class EntityValue extends Value
return entity;
}
public static ServerPlayerEntity getPlayerByValue(MinecraftServer server, Value value)
{
ServerPlayerEntity player = null;
if (value instanceof EntityValue)
{
Entity e = ((EntityValue) value).getEntity();
if (e instanceof ServerPlayerEntity)
{
player = (ServerPlayerEntity) e;
}
}
else
{
String playerName = value.getString();
player = server.getPlayerManager().getPlayer(playerName);
}
return player;
}
@Override
public String getString()
{
@@ -37,6 +37,11 @@ public class ThreadValue extends Value
return taskFuture.getNow(Value.NULL).getString();
}
public Value getValue()
{
return taskFuture.getNow(Value.NULL);
}
@Override
public boolean getBoolean()
{