Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
44c982a
Add wandering trader config
lonevox Jun 28, 2024
b11b42b
Fix config definition
lonevox Jun 28, 2024
b9bf3bf
Merge pull request #638 from lonevox/wandering-trader-config
SirEndii Jun 28, 2024
c1abd46
Add entity API
lonevox Jan 13, 2024
c58640c
Fix method name
lonevox Jan 13, 2024
0ef4e02
Change tabs to spaces
lonevox Jan 15, 2024
4c49819
Return UUID from more functions
lonevox Feb 4, 2024
9a0ca40
Move EntityAPI access to Environment Detector
lonevox Jun 28, 2024
1d9882d
Fix rebase issues
lonevox Jun 28, 2024
59cc2eb
Make nullableValue more generic
lonevox Jun 28, 2024
ee43762
Fix another rebase issue
lonevox Jun 28, 2024
073e14d
Fixed UUID fields in entity to lua conversions
lonevox Jun 28, 2024
0cca2a2
Add lastHurtByMob to livingEntityToLua
lonevox Jun 28, 2024
bb4ce77
Merge remote-tracking branch 'upstream/dev/0.8' into 1.19.2
lonevox Jun 28, 2024
6f1097b
Fix wandering trader config
lonevox Jun 28, 2024
bea8f88
Make more fields detailed
lonevox Jun 28, 2024
492afb3
Merge remote-tracking branch 'origin/1.19.2' into 1.19.2
lonevox Jun 28, 2024
826805b
Add detailed in entityToLua
lonevox Jun 28, 2024
8538430
only provide shearable field when needed
zyxkad Jun 28, 2024
13eb097
Fix calls to entityToLua
lonevox Jun 28, 2024
4d6c080
Change UUIDs back to strings
lonevox Jun 28, 2024
b69a45d
Make hasContainerOpen require detailed to be true
lonevox Jun 28, 2024
db19c51
Replace enablePlayerUUIDFunction with enablePlayerAccess
lonevox Jun 28, 2024
e44aafe
Remove unused imports
lonevox Jun 28, 2024
762fb36
Merge remote-tracking branch 'origin/1.19.2' into 1.19.2
lonevox Jun 29, 2024
fe7a743
Keep it simple
lonevox Jun 29, 2024
de46148
Return error for player access instead of throwing
lonevox Jun 29, 2024
b351587
Fix getting entity position
lonevox Jun 29, 2024
7632c49
Simplify searchAnimals
lonevox Jun 29, 2024
b78fbee
Reintroduce id and uuid to entityToLua
lonevox Jun 29, 2024
e8566c8
Add detailed argument
lonevox Jun 29, 2024
0d200ce
Fix return
lonevox Jun 29, 2024
5141dde
Fix checkstyle violations
lonevox Jun 29, 2024
fa7e08c
Merge remote-tracking branch 'origin/1.19.2' into 1.19.2
lonevox Jun 29, 2024
a5e691c
Fix getting argument in getData
lonevox Jun 29, 2024
e930d21
Handle error when entity doesn't exist
lonevox Jun 29, 2024
f99e718
Improve error handling in EntityAPI
lonevox Jun 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import de.srendi.advancedperipherals.common.util.EntityUtil;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;

import java.util.Map;
import java.util.UUID;

public class EntityAPI {

Expand All @@ -17,29 +19,35 @@ public class EntityAPI {
public final Object getNBT(IArguments arguments) throws LuaException {
if (!APConfig.API_CONFIG.enableGetNBT.get())
throw new LuaException("This function is disabled in the config. Activate it or ask an admin if they can activate it.");
Entity entity = EntityUtil.getEntityFromUUIDLua(arguments.getTable(0));
Entity entity = EntityUtil.getEntityFromUUID(UUID.fromString(arguments.getString(0)));
if (!APConfig.API_CONFIG.enablePlayerAccess.get() && entity instanceof Player)
throw new LuaException("Using players in EntityAPI is disabled in the config. Activate it or ask an admin if they can activate it.");
return NBTUtil.toLua(entity.serializeNBT());
}

@LuaFunction(mainThread = true)
public final Object getName(IArguments arguments) throws LuaException {
Entity entity = EntityUtil.getEntityFromUUIDLua(arguments.getTable(0));
Entity entity = EntityUtil.getEntityFromUUID(UUID.fromString(arguments.getString(0)));
return entity.getName().getString();
}

@LuaFunction(mainThread = true)
public final Map<String, Object> getBoundingBox(IArguments arguments) throws LuaException {
if (!APConfig.API_CONFIG.enableGetBoundingBox.get())
throw new LuaException("This function is disabled in the config. Activate it or ask an admin if they can activate it.");
Entity entity = EntityUtil.getEntityFromUUIDLua(arguments.getTable(0));
Entity entity = EntityUtil.getEntityFromUUID(UUID.fromString(arguments.getString(0)));
if (!APConfig.API_CONFIG.enablePlayerAccess.get() && entity instanceof Player)
throw new LuaException("Using players in EntityAPI is disabled in the config. Activate it or ask an admin if they can activate it.");
return LuaConverter.aabbToObject(entity.getBoundingBox());
}

@LuaFunction(mainThread = true)
public final Map<String, Object> getPos(IArguments arguments) throws LuaException {
if (!APConfig.API_CONFIG.enableGetPos.get())
throw new LuaException("This function is disabled in the config. Activate it or ask an admin if they can activate it.");
Entity entity = EntityUtil.getEntityFromUUIDLua(arguments.getTable(0));
Entity entity = EntityUtil.getEntityFromUUID(UUID.fromString(arguments.getString(0)));
if (!APConfig.API_CONFIG.enablePlayerAccess.get() && entity instanceof Player)
throw new LuaException("Using players in EntityAPI is disabled in the config. Activate it or ask an admin if they can activate it.");
return LuaConverter.vec3ToLua(entity.getPosition(1));
}

Expand All @@ -52,7 +60,9 @@ public final Map<String, Object> getPos(IArguments arguments) throws LuaExceptio
public final Map<String, Object> getData(IArguments arguments) throws LuaException {
if (!APConfig.API_CONFIG.enableGetData.get())
throw new LuaException("This function is disabled in the config. Activate it or ask an admin if they can activate it.");
Entity entity = EntityUtil.getEntityFromUUIDLua(arguments.getTable(0));
Entity entity = EntityUtil.getEntityFromUUID(UUID.fromString(arguments.getString(0)));
if (!APConfig.API_CONFIG.enablePlayerAccess.get() && entity instanceof Player)
throw new LuaException("Using players in EntityAPI is disabled in the config. Activate it or ask an admin if they can activate it.");
return LuaConverter.completeEntityToLua(entity);
}

Expand All @@ -63,7 +73,9 @@ public final Map<String, Object> getData(IArguments arguments) throws LuaExcepti
public final Object getPersistentData(IArguments arguments) throws LuaException {
if (!APConfig.API_CONFIG.enableGetPersistentData.get())
throw new LuaException("This function is disabled in the config. Activate it or ask an admin if they can activate it.");
Entity entity = EntityUtil.getEntityFromUUIDLua(arguments.getTable(0));
Entity entity = EntityUtil.getEntityFromUUID(UUID.fromString(arguments.getString(0)));
if (!APConfig.API_CONFIG.enablePlayerAccess.get() && entity instanceof Player)
throw new LuaException("Using players in EntityAPI is disabled in the config. Activate it or ask an admin if they can activate it.");
return NBTUtil.toLua(entity.getPersistentData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ public final MethodResult scanEntities(@NotNull IComputerAccess access, @NotNull
}, context -> {
BlockPos pos = owner.getPos();
AABB box = new AABB(pos);
List<Map<Integer, Object>> entityUUIDs = getLevel().getEntities((Entity) null, box.inflate(radius), LivingEntity.class::isInstance).stream()
.map(entity -> LuaConverter.uuidToLua(entity.getUUID()))
List<String> entityUUIDs = getLevel().getEntities((Entity) null, box.inflate(radius), LivingEntity.class::isInstance).stream()
.map(entity -> entity.getUUID().toString())
.toList();
return MethodResult.of(entityUUIDs);
}, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,16 @@ public final Map<String, Object> getPlayerPos(IArguments arguments) throws LuaEx
}

@LuaFunction(mainThread = true)
public final Map<Integer, Object> getPlayerUUID(String username) throws LuaException {
if (!APConfig.PERIPHERALS_CONFIG.enablePlayerUUIDFunction.get())
throw new LuaException("This function is disabled in the config. Activate it or ask an admin if they can activate it.");
public final String getPlayerUUID(String username) throws LuaException {
ResourceKey<Level> dimension = getLevel().dimension();

for (Player player : getPlayers()) {
if (!isAllowedMultiDimensional() && player.getLevel().dimension() != dimension)
continue;
if(player.getName().getString().equals(username))
return LuaConverter.uuidToLua(player.getUUID());
return player.getUUID().toString();
}
return Collections.emptyMap();
return null;
}

private List<ServerPlayer> getPlayers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public final MethodResult lookAtEntity(@NotNull IArguments arguments) throws Lua
return MethodResult.of(null, "No entity find");

EntityHitResult entityHit = (EntityHitResult) result;
return MethodResult.of(LuaConverter.entityToLua(entityHit.getEntity()));
return MethodResult.of(LuaConverter.entityToLua(entityHit.getEntity(), true));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class APIConfig implements IAPConfig {
public final ForgeConfigSpec.BooleanValue enableGetPos;
public final ForgeConfigSpec.BooleanValue enableGetData;
public final ForgeConfigSpec.BooleanValue enableGetPersistentData;
public final ForgeConfigSpec.BooleanValue enablePlayerAccess;
private final ForgeConfigSpec configSpec;

public APIConfig() {
Expand All @@ -26,6 +27,7 @@ public APIConfig() {
enableGetPos = builder.comment("Activates the \"getPos\" function of the entity API").define("enableGetPos", true);
enableGetData = builder.comment("Activates the \"getData\" function of the entity API").define("enableGetData", true);
enableGetPersistentData = builder.comment("Activates the \"getPersistentData\" function of the entity API").define("enableGetPersistentData", true);
enablePlayerAccess = builder.comment("Allows player entities to be used with the EntityAPI.").define("enablePlayerAccess", true);

builder.pop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class PeripheralsConfig implements IAPConfig {
// Player Detector
public final ForgeConfigSpec.IntValue playerDetMaxRange;
public final ForgeConfigSpec.BooleanValue enablePlayerPosFunction;
public final ForgeConfigSpec.BooleanValue enablePlayerUUIDFunction;
public final ForgeConfigSpec.BooleanValue morePlayerInformation;
public final ForgeConfigSpec.BooleanValue enablePlayerDetector;
public final ForgeConfigSpec.BooleanValue playerDetMultiDimensional;
Expand Down Expand Up @@ -138,7 +137,6 @@ public PeripheralsConfig() {
enablePlayerDetector = builder.comment("Enable the Player Detector or not.").define("enablePlayerDetector", true);
playerDetMaxRange = builder.comment("The max range of the player detector functions. If anyone use a higher range, the detector will use this max range. -1 for unlimited").defineInRange("playerDetMaxRange", -1, -1, Integer.MAX_VALUE);
enablePlayerPosFunction = builder.comment("Activates the \"getPlayerPos\" function of the Player Detector").define("enablePlayerPosFunction", true);
enablePlayerUUIDFunction = builder.comment("Activates the \"getPlayerUUID\" function of the Player Detector").define("enablePlayerUUIDFunction", true);
morePlayerInformation = builder.comment("Adds more information to `getPlayerPos` of the Player Detector. Like rotation and dimension").define("morePlayerInformation", true);
playerDetMultiDimensional = builder.comment("If true, the player detector can observe players which aren't in the same dimension as the detector itself. `playerDetMaxRange` needs to be infinite(-1) for it to work.").define("chatBoxMultiDimensional", true);
playerSpyRandError = builder.comment("If true, add random error to `getPlayerPos` player position that varies based on how far the player is from the detector. Prevents getting the exact position of players far from the detector.").define("enablePlayerPosRandomError", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,6 @@
import java.util.UUID;

public class EntityUtil {
/**
* A version of {@link EntityUtil#getEntityFromUUID} designed to be called from a
* {@link dan200.computercraft.api.lua.LuaFunction LuaFunction}.
* @param uuidList list containing 4 integers that together make a {@code UUID}
* @return an {@code Entity} that has the given {@code UUID} if one is found
* @throws LuaException throws if {@code uuidList} is incorrectly formatted or no {@code Entity} exists with the
* given {@code UUID}
*/
public static Entity getEntityFromUUIDLua(Map<?, ?> uuidList) throws LuaException {
try {
UUID uuid = LuaConverter.luaToUUID(uuidList);
return getEntityFromUUID(uuid);
} catch (IllegalArgumentException e) {
throw new LuaException(e.getMessage());
}
}

/**
* Searches all levels for an {@link Entity} with the given {@link UUID}.
* @param uuid the unique identifier of the {@code Entity}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import de.srendi.advancedperipherals.common.util.inventory.ItemUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Position;
import net.minecraft.core.UUIDUtil;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.tags.TagKey;
import net.minecraft.util.StringRepresentable;
Expand Down Expand Up @@ -37,7 +36,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand All @@ -63,7 +61,7 @@ public static Map<String, Object> entityToLua(Entity entity, boolean detailed) {
}

public static Map<String, Object> livingEntityToLua(LivingEntity entity, boolean detailed) {
Map<String, Object> data = entityToLua(entity);
Map<String, Object> data = entityToLua(entity, detailed);
data.put("baby", entity.isBaby());
data.put("health", entity.getHealth());
data.put("maxHealth", entity.getMaxHealth());
Expand All @@ -74,9 +72,9 @@ public static Map<String, Object> livingEntityToLua(LivingEntity entity, boolean
data.put("sensitiveToWater", entity.isSensitiveToWater());
if (detailed) {
data.put("noActionTime", entity.getNoActionTime());
data.put("lastHurtMob", entityToLuaUUID(entity.getLastHurtMob()));
data.put("lastHurtByMob", entityToLuaUUID(entity.getLastHurtByMob()));
data.put("killCredit", entityToLuaUUID(entity.getKillCredit()));
data.put("lastHurtMob", entityToUUIDString(entity.getLastHurtMob()));
data.put("lastHurtByMob", entityToUUIDString(entity.getLastHurtByMob()));
data.put("killCredit", entityToUUIDString(entity.getKillCredit()));
data.put("lastDamageSource", ObjectUtil.nullableValue(entity.getLastDamageSource(), LuaConverter::damageSourceToObject));
data.put("arrowCount", entity.getArrowCount());
data.put("stingerCount", entity.getStingerCount());
Expand Down Expand Up @@ -104,14 +102,14 @@ public static Map<String, Object> livingEntityToLua(LivingEntity entity, boolean
public static Map<String, Object> playerToLua(Player player, boolean detailed) {
Map<String, Object> data = livingEntityToLua(player, detailed);
data.put("secondaryUseActive", player.isSecondaryUseActive());
data.put("hasContainerOpen", player.hasContainerOpen());
data.put("xpNeededForNextLevel", player.getXpNeededForNextLevel());
data.put("creative", player.isCreative());
data.put("score", player.getScore());
data.put("luck", player.getLuck());
Inventory inv = player.getInventory();
data.put("handSlot", inv.selected);
if (detailed) {
data.put("hasContainerOpen", player.hasContainerOpen());
Map<Integer, Object> invMap = new HashMap<>();
for (int slot = 0; slot < inv.getContainerSize(); slot++) {
ItemStack item = inv.getItem(slot);
Expand All @@ -129,8 +127,8 @@ public static Map<String, Object> mobToLua(Mob mob, boolean detailed) {
data.put("noAI", mob.isNoAi());
data.put("leftHanded", mob.isLeftHanded());
data.put("aggressive", mob.isAggressive());
data.put("target", entityToLuaUUID(mob.getTarget()));
data.put("leashHolder", entityToLuaUUID(mob.getLeashHolder()));
data.put("target", entityToUUIDString(mob.getTarget()));
data.put("leashHolder", entityToUUIDString(mob.getLeashHolder()));
data.put("spawnType", ObjectUtil.nullableValue(mob.getSpawnType(), Enum::name));
return data;
}
Expand Down Expand Up @@ -172,7 +170,7 @@ public static Map<String, Object> completeEntityToLua(Entity entity, ItemStack i
if (entity instanceof Mob mob) return mobToLua(mob, detailed);
if (entity instanceof Player player) return playerToLua(player, detailed);
if (entity instanceof LivingEntity livingEntity) return livingEntityToLua(livingEntity, detailed);
return entityToLua(entity);
return entityToLua(entity, detailed);
}

public static Map<String, Object> completeEntityWithRelativePositionToLua(Entity entity, BlockPos pos) {
Expand Down Expand Up @@ -320,8 +318,8 @@ public static Map<String, Object> positionToObject(@NotNull Position position) {
public static Map<String, Object> damageSourceToObject(@NotNull DamageSource damageSource) {
Map<String, Object> map = new HashMap<>();
map.put("msgId", damageSource.getMsgId());
map.put("entity", entityToLuaUUID(damageSource.getEntity()));
map.put("directEntity", entityToLuaUUID(damageSource.getDirectEntity()));
map.put("entity", entityToUUIDString(damageSource.getEntity()));
map.put("directEntity", entityToUUIDString(damageSource.getDirectEntity()));
map.put("foodExhaustion", damageSource.getFoodExhaustion());
map.put("sourcePosition", ObjectUtil.nullableValue(damageSource.getSourcePosition(), LuaConverter::positionToObject));
return map;
Expand Down Expand Up @@ -353,25 +351,6 @@ public static Map<String, Object> aabbToObject(@NotNull AABB aabb) {
return map;
}

public static Map<Integer, Object> uuidToLua(UUID uuid) {
int[] ints = UUIDUtil.uuidToIntArray(uuid);
Map<Integer, Object> data = new HashMap<>();
for (int i = 0; i < ints.length; i++) {
data.put(i + 1, ints[i]);
}
return data;
}

public static UUID luaToUUID(Map<?, ?> uuidList) {
int msb1 = (int) (double) uuidList.get(1.);
int msb2 = (int) (double) uuidList.get(2.);
int lsb1 = (int) (double) uuidList.get(3.);
int lsb2 = (int) (double) uuidList.get(4.);
long msb = (((long) msb1) << 32) | (msb2 & 0xffffffffL);
long lsb = (((long) lsb1) << 32) | (lsb2 & 0xffffffffL);
return new UUID(msb, lsb);
}

public static <T> List<String> tagsToList(@NotNull Supplier<Stream<TagKey<T>>> tags) {
if (tags.get().findAny().isEmpty()) return Collections.emptyList();
return tags.get().map(LuaConverter::tagToString).toList();
Expand Down Expand Up @@ -405,7 +384,8 @@ public static Object effectToObject(MobEffectInstance effect) {
return map;
}

private static Map<Integer, Object> entityToLuaUUID(Entity entity) {
return ObjectUtil.nullableValue(entity, e -> LuaConverter.uuidToLua(e.getUUID()));
@Nullable
private static String entityToUUIDString(@Nullable Entity entity) {
return ObjectUtil.nullableValue(entity, e -> e.getUUID().toString());
}
}