Use newer Java features in places where it makes sense and more (#1009)
This commit is contained in:
1 parent
28cb136c6b
commit
7fb5e47da5
34 files changed
+338
-399
No files matched your search
@@ -37,8 +37,6 @@ import carpet.script.value.StringValue;
|
||||
import carpet.script.value.Value;
|
||||
import carpet.script.value.ValueConversions;
|
||||
import carpet.utils.Messenger;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.block.BlockState;
|
||||
@@ -103,7 +101,6 @@ import net.minecraft.world.gen.chunk.ChunkGenerator;
|
||||
import net.minecraft.world.level.UnmodifiableLevelProperties;
|
||||
import net.minecraft.world.level.storage.LevelStorage;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
@@ -333,7 +330,7 @@ public class Auxiliary {
|
||||
ServerWorld world = cc.s.getWorld();
|
||||
MinecraftServer server = world.getServer();
|
||||
Set<ServerPlayerEntity> playerTargets = new HashSet<>();
|
||||
List<Pair<ShapeDispatcher.ExpiringShape, Map<String,Value>>> shapes = new ArrayList<>();
|
||||
List<ShapeDispatcher.ShapeWithConfig> shapes = new ArrayList<>();
|
||||
if (lv.size() == 1) // bulk
|
||||
{
|
||||
Value specLoad = lv.get(0);
|
||||
@@ -1061,10 +1058,10 @@ public class Auxiliary {
|
||||
{
|
||||
try {
|
||||
//Files.createDirectory(packFloder);
|
||||
try (FileSystem zipfs = FileSystems.newFileSystem(URI.create("jar:" + packFloder.toUri().toString()), ImmutableMap.of("create", "true"))) {
|
||||
try (FileSystem zipfs = FileSystems.newFileSystem(URI.create("jar:" + packFloder.toUri().toString()), Map.of("create", "true"))) {
|
||||
Path zipRoot = zipfs.getPath("/");
|
||||
zipValueToJson(zipRoot.resolve("pack.mcmeta"), MapValue.wrap(
|
||||
ImmutableMap.of(StringValue.of("pack"), MapValue.wrap(ImmutableMap.of(
|
||||
Map.of(StringValue.of("pack"), MapValue.wrap(Map.of(
|
||||
StringValue.of("pack_format"), new NumericValue(SharedConstants.getGameVersion().getPackVersion()),
|
||||
StringValue.of("description"), StringValue.of(name),
|
||||
StringValue.of("source"), StringValue.of("scarpet")
|
||||
@@ -1143,7 +1140,7 @@ public class Auxiliary {
|
||||
DimensionType dimensionType3 = entry.getValue().getDimensionType();
|
||||
ChunkGenerator chunkGenerator3 = entry.getValue().getChunkGenerator();
|
||||
UnmodifiableLevelProperties unmodifiableLevelProperties = new UnmodifiableLevelProperties(saveProperties, ((ServerWorldInterface) server.getOverworld()).getWorldPropertiesCM());
|
||||
ServerWorld serverWorld2 = new ServerWorld(server, Util.getMainWorkerExecutor(), session, unmodifiableLevelProperties, registryKey2, dimensionType3, WorldTools.NOOP_LISTENER, chunkGenerator3, bl, m, ImmutableList.of(), false);
|
||||
ServerWorld serverWorld2 = new ServerWorld(server, Util.getMainWorkerExecutor(), session, unmodifiableLevelProperties, registryKey2, dimensionType3, WorldTools.NOOP_LISTENER, chunkGenerator3, bl, m, List.of(), false);
|
||||
server.getOverworld().getWorldBorder().addListener(new WorldBorderListener.WorldBorderSyncer(serverWorld2.getWorldBorder()));
|
||||
existing_worlds.put(registryKey2, serverWorld2);
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ public class Inventories {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null) return Value.NULL;
|
||||
return new NumericValue(inventoryLocator.inventory.size());
|
||||
return new NumericValue(inventoryLocator.inventory().size());
|
||||
});
|
||||
|
||||
expression.addContextFunction("inventory_has_items", -1, (c, t, lv) ->
|
||||
@@ -189,7 +189,7 @@ public class Inventories {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null) return Value.NULL;
|
||||
return BooleanValue.of(!inventoryLocator.inventory.isEmpty());
|
||||
return BooleanValue.of(!inventoryLocator.inventory().isEmpty());
|
||||
});
|
||||
|
||||
//inventory_get(<b, e>, <n>) -> item_triple
|
||||
@@ -198,17 +198,17 @@ public class Inventories {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null) return Value.NULL;
|
||||
if (lv.size() == inventoryLocator.offset)
|
||||
if (lv.size() == inventoryLocator.offset())
|
||||
{
|
||||
List<Value> fullInventory = new ArrayList<>();
|
||||
for (int i = 0, maxi = inventoryLocator.inventory.size(); i < maxi; i++)
|
||||
fullInventory.add(ValueConversions.of(inventoryLocator.inventory.getStack(i)));
|
||||
for (int i = 0, maxi = inventoryLocator.inventory().size(); i < maxi; i++)
|
||||
fullInventory.add(ValueConversions.of(inventoryLocator.inventory().getStack(i)));
|
||||
return ListValue.wrap(fullInventory);
|
||||
}
|
||||
int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset)).getLong();
|
||||
slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory);
|
||||
if (slot == inventoryLocator.inventory.size()) return Value.NULL;
|
||||
return ValueConversions.of(inventoryLocator.inventory.getStack(slot));
|
||||
int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset())).getLong();
|
||||
slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory());
|
||||
if (slot == inventoryLocator.inventory().size()) return Value.NULL;
|
||||
return ValueConversions.of(inventoryLocator.inventory().getStack(slot));
|
||||
});
|
||||
|
||||
//inventory_set(<b,e>, <n>, <count>, <item>, <nbt>)
|
||||
@@ -217,33 +217,33 @@ public class Inventories {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null) return Value.NULL;
|
||||
if (lv.size() < inventoryLocator.offset+2)
|
||||
if (lv.size() < inventoryLocator.offset()+2)
|
||||
throw new InternalExpressionException("'inventory_set' requires at least slot number and new stack size, and optional new item");
|
||||
int slot = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset+0)).getLong();
|
||||
slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory);
|
||||
if (slot == inventoryLocator.inventory.size()) return Value.NULL;
|
||||
int count = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset+1)).getLong();
|
||||
int slot = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset()+0)).getLong();
|
||||
slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory());
|
||||
if (slot == inventoryLocator.inventory().size()) return Value.NULL;
|
||||
int count = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset()+1)).getLong();
|
||||
if (count == 0)
|
||||
{
|
||||
// clear slot
|
||||
ItemStack removedStack = inventoryLocator.inventory.removeStack(slot);
|
||||
ItemStack removedStack = inventoryLocator.inventory().removeStack(slot);
|
||||
syncPlayerInventory(inventoryLocator, slot);
|
||||
//Value res = ListValue.fromItemStack(removedStack); // that tuple will be read only but cheaper if noone cares
|
||||
return ValueConversions.of(removedStack);
|
||||
}
|
||||
if (lv.size() < inventoryLocator.offset+3)
|
||||
if (lv.size() < inventoryLocator.offset()+3)
|
||||
{
|
||||
ItemStack previousStack = inventoryLocator.inventory.getStack(slot);
|
||||
ItemStack previousStack = inventoryLocator.inventory().getStack(slot);
|
||||
ItemStack newStack = previousStack.copy();
|
||||
newStack.setCount(count);
|
||||
inventoryLocator.inventory.setStack(slot, newStack);
|
||||
inventoryLocator.inventory().setStack(slot, newStack);
|
||||
syncPlayerInventory(inventoryLocator, slot);
|
||||
return ValueConversions.of(previousStack);
|
||||
}
|
||||
NbtCompound nbt = null; // skipping one argument
|
||||
if (lv.size() > inventoryLocator.offset+3)
|
||||
if (lv.size() > inventoryLocator.offset()+3)
|
||||
{
|
||||
Value nbtValue = lv.get(inventoryLocator.offset+3);
|
||||
Value nbtValue = lv.get(inventoryLocator.offset()+3);
|
||||
if (nbtValue instanceof NBTSerializableValue)
|
||||
nbt = ((NBTSerializableValue)nbtValue).getCompoundTag();
|
||||
else if (nbtValue instanceof NullValue)
|
||||
@@ -251,11 +251,11 @@ public class Inventories {
|
||||
else
|
||||
nbt = new NBTSerializableValue(nbtValue.getString()).getCompoundTag();
|
||||
}
|
||||
ItemStackArgument newitem = NBTSerializableValue.parseItem(lv.get(inventoryLocator.offset+2).getString(), nbt);
|
||||
ItemStack previousStack = inventoryLocator.inventory.getStack(slot);
|
||||
ItemStackArgument newitem = NBTSerializableValue.parseItem(lv.get(inventoryLocator.offset()+2).getString(), nbt);
|
||||
ItemStack previousStack = inventoryLocator.inventory().getStack(slot);
|
||||
try
|
||||
{
|
||||
inventoryLocator.inventory.setStack(slot, newitem.createStack(count, false));
|
||||
inventoryLocator.inventory().setStack(slot, newitem.createStack(count, false));
|
||||
syncPlayerInventory(inventoryLocator, slot);
|
||||
}
|
||||
catch (CommandSyntaxException e)
|
||||
@@ -272,21 +272,21 @@ public class Inventories {
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null) return Value.NULL;
|
||||
ItemStackArgument itemArg = null;
|
||||
if (lv.size() > inventoryLocator.offset)
|
||||
if (lv.size() > inventoryLocator.offset())
|
||||
{
|
||||
Value secondArg = lv.get(inventoryLocator.offset+0);
|
||||
Value secondArg = lv.get(inventoryLocator.offset()+0);
|
||||
if (!(secondArg instanceof NullValue))
|
||||
itemArg = NBTSerializableValue.parseItem(secondArg.getString());
|
||||
}
|
||||
int startIndex = 0;
|
||||
if (lv.size() > inventoryLocator.offset+1)
|
||||
if (lv.size() > inventoryLocator.offset()+1)
|
||||
{
|
||||
startIndex = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset+1)).getLong();
|
||||
startIndex = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset()+1)).getLong();
|
||||
}
|
||||
startIndex = NBTSerializableValue.validateSlot(startIndex, inventoryLocator.inventory);
|
||||
for (int i = startIndex, maxi = inventoryLocator.inventory.size(); i < maxi; i++)
|
||||
startIndex = NBTSerializableValue.validateSlot(startIndex, inventoryLocator.inventory());
|
||||
for (int i = startIndex, maxi = inventoryLocator.inventory().size(); i < maxi; i++)
|
||||
{
|
||||
ItemStack stack = inventoryLocator.inventory.getStack(i);
|
||||
ItemStack stack = inventoryLocator.inventory().getStack(i);
|
||||
if ( (itemArg == null && stack.isEmpty()) || (itemArg != null && itemArg.getItem().equals(stack.getItem())) )
|
||||
return new NumericValue(i);
|
||||
}
|
||||
@@ -299,18 +299,18 @@ public class Inventories {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null) return Value.NULL;
|
||||
if (lv.size() <= inventoryLocator.offset)
|
||||
if (lv.size() <= inventoryLocator.offset())
|
||||
throw new InternalExpressionException("'inventory_remove' requires at least an item to be removed");
|
||||
ItemStackArgument searchItem = NBTSerializableValue.parseItem(lv.get(inventoryLocator.offset).getString());
|
||||
ItemStackArgument searchItem = NBTSerializableValue.parseItem(lv.get(inventoryLocator.offset()).getString());
|
||||
int amount = 1;
|
||||
if (lv.size() > inventoryLocator.offset+1)
|
||||
amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset+1)).getLong();
|
||||
if (lv.size() > inventoryLocator.offset()+1)
|
||||
amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset()+1)).getLong();
|
||||
// not enough
|
||||
if (((amount == 1) && (!inventoryLocator.inventory.containsAny(Sets.newHashSet(searchItem.getItem()))))
|
||||
|| (inventoryLocator.inventory.count(searchItem.getItem()) < amount)) return Value.FALSE;
|
||||
for (int i = 0, maxi = inventoryLocator.inventory.size(); i < maxi; i++)
|
||||
if (((amount == 1) && (!inventoryLocator.inventory().containsAny(Sets.newHashSet(searchItem.getItem()))))
|
||||
|| (inventoryLocator.inventory().count(searchItem.getItem()) < amount)) return Value.FALSE;
|
||||
for (int i = 0, maxi = inventoryLocator.inventory().size(); i < maxi; i++)
|
||||
{
|
||||
ItemStack stack = inventoryLocator.inventory.getStack(i);
|
||||
ItemStack stack = inventoryLocator.inventory().getStack(i);
|
||||
if (stack.isEmpty())
|
||||
continue;
|
||||
if (!stack.getItem().equals(searchItem.getItem()))
|
||||
@@ -319,13 +319,13 @@ public class Inventories {
|
||||
if (left > 0)
|
||||
{
|
||||
stack.setCount(left);
|
||||
inventoryLocator.inventory.setStack(i, stack);
|
||||
inventoryLocator.inventory().setStack(i, stack);
|
||||
syncPlayerInventory(inventoryLocator, i);
|
||||
return Value.TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
inventoryLocator.inventory.removeStack(i);
|
||||
inventoryLocator.inventory().removeStack(i);
|
||||
syncPlayerInventory(inventoryLocator, i);
|
||||
amount -= stack.getCount();
|
||||
}
|
||||
@@ -341,22 +341,22 @@ public class Inventories {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null) return Value.NULL;
|
||||
if (lv.size() == inventoryLocator.offset)
|
||||
if (lv.size() == inventoryLocator.offset())
|
||||
throw new InternalExpressionException("Slot number is required for inventory_drop");
|
||||
int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset)).getLong();
|
||||
slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory);
|
||||
if (slot == inventoryLocator.inventory.size()) return Value.NULL;
|
||||
int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset())).getLong();
|
||||
slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory());
|
||||
if (slot == inventoryLocator.inventory().size()) return Value.NULL;
|
||||
int amount = 0;
|
||||
if (lv.size() > inventoryLocator.offset+1)
|
||||
amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset+1)).getLong();
|
||||
if (lv.size() > inventoryLocator.offset()+1)
|
||||
amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset()+1)).getLong();
|
||||
if (amount < 0)
|
||||
throw new InternalExpressionException("Cannot throw negative number of items");
|
||||
ItemStack stack = inventoryLocator.inventory.getStack(slot);
|
||||
ItemStack stack = inventoryLocator.inventory().getStack(slot);
|
||||
if (stack == null || stack.isEmpty()) return Value.ZERO;
|
||||
if (amount == 0) amount = stack.getCount();
|
||||
ItemStack droppedStack = inventoryLocator.inventory.removeStack(slot, amount);
|
||||
ItemStack droppedStack = inventoryLocator.inventory().removeStack(slot, amount);
|
||||
if (droppedStack.isEmpty()) return Value.ZERO;
|
||||
Object owner = inventoryLocator.owner;
|
||||
Object owner = inventoryLocator.owner();
|
||||
ItemEntity item;
|
||||
if (owner instanceof PlayerEntity)
|
||||
{
|
||||
@@ -374,7 +374,7 @@ public class Inventories {
|
||||
}
|
||||
else
|
||||
{
|
||||
Vec3d point = Vec3d.ofCenter(inventoryLocator.position); //pos+0.5v
|
||||
Vec3d point = Vec3d.ofCenter(inventoryLocator.position()); //pos+0.5v
|
||||
item = new ItemEntity(cc.s.getWorld(), point.x, point.y, point.z, droppedStack);
|
||||
item.setToDefaultPickupDelay();
|
||||
cc.s.getWorld().spawnEntity(item);
|
||||
@@ -385,12 +385,12 @@ public class Inventories {
|
||||
|
||||
private static void syncPlayerInventory(NBTSerializableValue.InventoryLocator inventory, int int_1)
|
||||
{
|
||||
if (inventory.owner instanceof ServerPlayerEntity player && !inventory.isEnder)
|
||||
if (inventory.owner() instanceof ServerPlayerEntity player && !inventory.isEnder())
|
||||
{
|
||||
player.networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(
|
||||
-2, 0, // resolve mystery argument
|
||||
int_1,
|
||||
inventory.inventory.getStack(int_1)
|
||||
inventory.inventory().getStack(int_1)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package carpet.script.api;
|
||||
import carpet.fakes.SpawnHelperInnerInterface;
|
||||
import carpet.script.CarpetContext;
|
||||
import carpet.script.Expression;
|
||||
import carpet.script.LazyValue;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.utils.SystemInfo;
|
||||
import carpet.script.value.ListValue;
|
||||
|
||||
Reference in new issue
Block a user