cleanup of scarpet dependencies

This commit is contained in:
gnembon committed 2023-02-08 21:13:14 +01:00
1 parent fd93785fec
commit 75d94b0570
71 files changed
+1623 -1014

No files matched your search

+27 -30
View File
@@ -1,10 +1,7 @@
package carpet.script.api;
import carpet.CarpetServer;
import carpet.fakes.MinecraftServerInterface;
import carpet.fakes.ThreadedAnvilChunkStorageInterface;
import carpet.helpers.FeatureGenerator;
import carpet.logging.HUDController;
import carpet.script.external.Vanilla;
import carpet.script.utils.FeatureGenerator;
import carpet.script.argument.FileArgument;
import carpet.script.CarpetContext;
import carpet.script.CarpetEventServer;
@@ -19,6 +16,7 @@ import carpet.script.exception.ExitStatement;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement;
import carpet.script.exception.Throwables;
import carpet.script.external.Carpet;
import carpet.script.utils.SnoopyCommandSource;
import carpet.script.utils.SystemInfo;
import carpet.script.utils.InputValidator;
@@ -36,7 +34,6 @@ import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import carpet.script.value.ValueConversions;
import carpet.utils.Messenger;
import com.google.common.collect.Lists;
import net.minecraft.SharedConstants;
import net.minecraft.commands.CommandSourceStack;
@@ -73,7 +70,6 @@ import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
@@ -613,11 +609,11 @@ public class Auxiliary
final Map<String, Component> map;
if (actionString.equals("player_list_header"))
{
map = HUDController.scarpet_headers;
map = Carpet.getScarpetHeaders();
}
else
{
map = HUDController.scarpet_footers;
map = Carpet.getScarpetFooters();
}
final AtomicInteger total = new AtomicInteger(0);
@@ -636,7 +632,7 @@ public class Auxiliary
total.getAndIncrement();
});
}
HUDController.update_hud(((CarpetContext) c).server(), targetList);
Carpet.updateScarpetHUDs(((CarpetContext) c).server(), targetList);
return NumericValue.of(total.get());
}
final ClientboundSetTitlesAnimationPacket timesPacket; // TimesPacket
@@ -678,7 +674,7 @@ public class Auxiliary
{
values = list.getItems();
}
return new FormattedTextValue(Messenger.c(values.stream().map(Value::getString).toArray()));
return new FormattedTextValue(Carpet.Messenger_compose(values.stream().map(Value::getString).toArray()));
});
expression.addContextFunction("run", 1, (c, t, lv) ->
@@ -750,7 +746,8 @@ public class Auxiliary
expression.addContextFunction("game_tick", -1, (c, t, lv) -> {
final CarpetContext cc = (CarpetContext) c;
final MinecraftServer server = cc.server();
if (CarpetServer.scriptServer == null)
final CarpetScriptServer scriptServer = (CarpetScriptServer) c.host.scriptServer();
if (scriptServer == null)
{
return Value.NULL;
}
@@ -758,18 +755,18 @@ public class Auxiliary
{
throw new InternalExpressionException("Unable to run ticks from threads");
}
if (CarpetServer.scriptServer.tickDepth > 16)
if (scriptServer.tickDepth > 16)
{
throw new InternalExpressionException("'game_tick' function caused other 'game_tick' functions to run. You should not allow that.");
}
try
{
CarpetServer.scriptServer.tickDepth++;
((MinecraftServerInterface) server).forceTick(() -> System.nanoTime() - CarpetServer.scriptServer.tickStart < 50000000L);
scriptServer.tickDepth++;
Vanilla.MinecraftServer_forceTick(server, () -> System.nanoTime() - scriptServer.tickStart < 50000000L);
if (lv.size() > 0)
{
final long ms_total = NumericValue.asNumber(lv.get(0)).getLong();
final long end_expected = CarpetServer.scriptServer.tickStart + ms_total * 1000000L;
final long end_expected = scriptServer.tickStart + ms_total * 1000000L;
final long wait = end_expected - System.nanoTime();
if (wait > 0L)
{
@@ -782,17 +779,17 @@ public class Auxiliary
}
}
}
CarpetServer.scriptServer.tickStart = System.nanoTime(); // for the next tick
scriptServer.tickStart = System.nanoTime(); // for the next tick
Thread.yield();
}
finally
{
if (CarpetServer.scriptServer != null)
if (scriptServer != null)
{
CarpetServer.scriptServer.tickDepth--;
scriptServer.tickDepth--;
}
}
if (CarpetServer.scriptServer != null && CarpetServer.scriptServer.stopAll)
if (scriptServer != null && scriptServer.stopAll)
{
throw new ExitStatement(Value.NULL);
}
@@ -811,7 +808,7 @@ public class Auxiliary
final BlockArgument locator = BlockArgument.findIn(cc, lv, 0);
final BlockPos pos = locator.block.getPos();
final ServerLevel world = cc.level();
((ThreadedAnvilChunkStorageInterface) world.getChunkSource().chunkMap).relightChunk(new ChunkPos(pos));
Vanilla.ChunkMap_relightChunk(world.getChunkSource().chunkMap, new ChunkPos(pos));
WorldTools.forceChunkUpdate(pos, world);
return Value.TRUE;
});
@@ -896,7 +893,7 @@ public class Auxiliary
final long delay = NumericValue.asNumber(lv.get(0)).getLong();
final FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false, false);
CarpetServer.scriptServer.events.scheduleCall(
((CarpetScriptServer)c.host.scriptServer()).events.scheduleCall(
(CarpetContext) c,
functionArgument.function,
functionArgument.checkedArgs(),
@@ -938,14 +935,14 @@ public class Auxiliary
expression.addContextFunction("list_files", 2, (c, t, lv) ->
{
final FileArgument fdesc = FileArgument.from(lv, true, FileArgument.Reason.READ);
final FileArgument fdesc = FileArgument.from(c, lv, true, FileArgument.Reason.READ);
final Stream<String> files = ((CarpetScriptHost) c.host).listFolder(fdesc);
return files == null ? Value.NULL : ListValue.wrap(files.map(StringValue::of));
});
expression.addContextFunction("read_file", 2, (c, t, lv) ->
{
final FileArgument fdesc = FileArgument.from(lv, false, FileArgument.Reason.READ);
final FileArgument fdesc = FileArgument.from(c, lv, false, FileArgument.Reason.READ);
if (fdesc.type == FileArgument.Type.NBT)
{
final Tag state = ((CarpetScriptHost) c.host).readFileTag(fdesc);
@@ -966,14 +963,14 @@ public class Auxiliary
});
expression.addContextFunction("delete_file", 2, (c, t, lv) ->
BooleanValue.of(((CarpetScriptHost) c.host).removeResourceFile(FileArgument.from(lv, false, FileArgument.Reason.DELETE))));
BooleanValue.of(((CarpetScriptHost) c.host).removeResourceFile(FileArgument.from(c, lv, false, FileArgument.Reason.DELETE))));
expression.addContextFunction("write_file", -1, (c, t, lv) -> {
if (lv.size() < 3)
{
throw new InternalExpressionException("'write_file' requires three or more arguments");
}
final FileArgument fdesc = FileArgument.from(lv, false, FileArgument.Reason.CREATE);
final FileArgument fdesc = FileArgument.from(c, lv, false, FileArgument.Reason.CREATE);
final boolean success;
if (fdesc.type == FileArgument.Type.NBT)
@@ -1021,13 +1018,13 @@ public class Auxiliary
expression.addContextFunction("load_app_data", -1, (c, t, lv) ->
{
FileArgument fdesc = new FileArgument(null, FileArgument.Type.NBT, null, false, false, FileArgument.Reason.READ);
FileArgument fdesc = new FileArgument(null, FileArgument.Type.NBT, null, false, false, FileArgument.Reason.READ, c.host);
if (lv.size() > 0)
{
c.host.issueDeprecation("load_app_data(...) with arguments");
final String resource = recognizeResource(lv.get(0), false);
final boolean shared = lv.size() > 1 && lv.get(1).getBoolean();
fdesc = new FileArgument(resource, FileArgument.Type.NBT, null, false, shared, FileArgument.Reason.READ);
fdesc = new FileArgument(resource, FileArgument.Type.NBT, null, false, shared, FileArgument.Reason.READ, c.host);
}
return NBTSerializableValue.of(((CarpetScriptHost) c.host).readFileTag(fdesc));
});
@@ -1039,13 +1036,13 @@ public class Auxiliary
throw new InternalExpressionException("'store_app_data' needs NBT tag and an optional file");
}
final Value val = lv.get(0);
FileArgument fdesc = new FileArgument(null, FileArgument.Type.NBT, null, false, false, FileArgument.Reason.CREATE);
FileArgument fdesc = new FileArgument(null, FileArgument.Type.NBT, null, false, false, FileArgument.Reason.CREATE, c.host);
if (lv.size() > 1)
{
c.host.issueDeprecation("store_app_data(...) with more than one argument");
final String resource = recognizeResource(lv.get(1), false);
final boolean shared = lv.size() > 2 && lv.get(2).getBoolean();
fdesc = new FileArgument(resource, FileArgument.Type.NBT, null, false, shared, FileArgument.Reason.CREATE);
fdesc = new FileArgument(resource, FileArgument.Type.NBT, null, false, shared, FileArgument.Reason.CREATE, c.host);
}
final NBTSerializableValue tagValue = (val instanceof final NBTSerializableValue nbtsv)
? nbtsv
@@ -1,13 +1,12 @@
package carpet.script.api;
import carpet.fakes.IngredientInterface;
import carpet.fakes.RecipeManagerInterface;
import carpet.script.CarpetContext;
import carpet.script.Expression;
import carpet.script.argument.FunctionArgument;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement;
import carpet.script.exception.Throwables;
import carpet.script.external.Vanilla;
import carpet.script.utils.InputValidator;
import carpet.script.value.BooleanValue;
import carpet.script.value.EntityValue;
@@ -111,7 +110,7 @@ public class Inventories
final String recipeType = lv.get(1).getString();
type = cc.registry(Registries.RECIPE_TYPE).get(InputValidator.identifierOf(recipeType));
}
final List<Recipe<?>> recipes = ((RecipeManagerInterface) cc.server().getRecipeManager()).getAllMatching(type, InputValidator.identifierOf(recipeName), cc.registryAccess());
final List<Recipe<?>> recipes = Vanilla.RecipeManager_getAllMatching(cc.server().getRecipeManager(), type, InputValidator.identifierOf(recipeName), cc.registryAccess());
if (recipes.isEmpty())
{
return Value.NULL;
@@ -126,7 +125,7 @@ public class Inventories
// I am flattening ingredient lists per slot.
// consider recipe_data('wooden_sword','crafting') and ('iron_nugget', 'blasting') and notice difference
// in depths of lists.
final List<Collection<ItemStack>> stacks = ((IngredientInterface) (Object) ingredient).getRecipeStacks();
final List<Collection<ItemStack>> stacks = Vanilla.Ingredient_getRecipeStacks(ingredient);
if (stacks.isEmpty())
{
ingredientValue.add(Value.NULL);
@@ -3,13 +3,13 @@ package carpet.script.api;
import carpet.script.CarpetContext;
import carpet.script.Expression;
import carpet.script.exception.InternalExpressionException;
import carpet.script.external.Vanilla;
import carpet.script.utils.SystemInfo;
import carpet.script.value.ListValue;
import carpet.script.value.MapValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import carpet.utils.SpawnReporter;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import java.util.Arrays;
@@ -64,7 +64,7 @@ public class Monitoring
final Map<Value, Value> retDict = new HashMap<>();
for (final MobCategory category : mobcounts.keySet())
{
final int currentCap = category.getMaxInstancesPerChunk() * chunks / SpawnReporter.MAGIC_NUMBER;
final int currentCap = category.getMaxInstancesPerChunk() * chunks / Vanilla.NaturalSpawner_MAGIC_NUMBER();
retDict.put(
new StringValue(category.getSerializedName().toLowerCase(Locale.ROOT)),
ListValue.of(
@@ -82,7 +82,7 @@ public class Monitoring
}
return ListValue.of(
new NumericValue(mobcounts.getInt(cat)),
new NumericValue((int) (cat.getMaxInstancesPerChunk() * chunks / SpawnReporter.MAGIC_NUMBER))
new NumericValue((int) (cat.getMaxInstancesPerChunk() * chunks / Vanilla.NaturalSpawner_MAGIC_NUMBER()))
);
});
}
@@ -1,12 +1,11 @@
package carpet.script.api;
import carpet.mixins.Objective_scarpetMixin;
import carpet.mixins.Scoreboard_scarpetMixin;
import carpet.script.CarpetContext;
import carpet.script.Expression;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement;
import carpet.script.exception.Throwables;
import carpet.script.external.Vanilla;
import carpet.script.utils.InputValidator;
import carpet.script.value.BooleanValue;
import carpet.script.value.EntityValue;
@@ -164,9 +163,9 @@ public class Scoreboards
{
return Value.NULL;
}
((Scoreboard_scarpetMixin) scoreboard).getObjectivesByCriterion().get(objective.getCriteria()).remove(objective);
((Objective_scarpetMixin) objective).setCriterion(criterion);
(((Scoreboard_scarpetMixin) scoreboard).getObjectivesByCriterion().computeIfAbsent(criterion, (criterion1) -> Lists.newArrayList())).add(objective);
Vanilla.Scoreboard_getObjectivesByCriterion(scoreboard).get(objective.getCriteria()).remove(objective);
Vanilla.Objective_setCriterion(objective, criterion);
(Vanilla.Scoreboard_getObjectivesByCriterion(scoreboard).computeIfAbsent(criterion, (criterion1) -> Lists.newArrayList())).add(objective);
scoreboard.onObjectiveAdded(objective);
return Value.FALSE;
}
@@ -209,9 +208,9 @@ public class Scoreboards
{
return Value.FALSE;
}
((Scoreboard_scarpetMixin) scoreboard).getObjectivesByCriterion().get(objective.getCriteria()).remove(objective);
((Objective_scarpetMixin) objective).setCriterion(criterion);
(((Scoreboard_scarpetMixin) scoreboard).getObjectivesByCriterion().computeIfAbsent(criterion, (criterion1) -> Lists.newArrayList())).add(objective);
Vanilla.Scoreboard_getObjectivesByCriterion(scoreboard).get(objective.getCriteria()).remove(objective);
Vanilla.Objective_setCriterion(objective, criterion);
(Vanilla.Scoreboard_getObjectivesByCriterion(scoreboard).computeIfAbsent(criterion, (criterion1) -> Lists.newArrayList())).add(objective);
scoreboard.onObjectiveAdded(objective);
return Value.TRUE;
}
@@ -1,21 +1,14 @@
package carpet.script.api;
import carpet.CarpetSettings;
import carpet.fakes.ChunkGeneratorInterface;
import carpet.fakes.ChunkTicketManagerInterface;
import carpet.fakes.RandomStateVisitorAccessor;
import carpet.fakes.ServerChunkManagerInterface;
import carpet.fakes.ServerWorldInterface;
import carpet.fakes.SpawnHelperInnerInterface;
import carpet.fakes.ThreadedAnvilChunkStorageInterface;
import carpet.helpers.FeatureGenerator;
import carpet.mixins.PoiRecord_scarpetMixin;
import carpet.script.CarpetContext;
import carpet.script.CarpetScriptServer;
import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.Fluff;
import carpet.script.annotation.Locator;
import carpet.script.annotation.ScarpetFunction;
import carpet.script.external.Carpet;
import carpet.script.external.Vanilla;
import carpet.script.utils.FeatureGenerator;
import carpet.mixins.PoiRecord_scarpetMixin;
import carpet.script.argument.BlockArgument;
import carpet.script.argument.Vector3Argument;
import carpet.script.exception.InternalExpressionException;
@@ -34,7 +27,6 @@ import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import carpet.script.value.ValueConversions;
import carpet.utils.BlockInfo;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
@@ -48,6 +40,11 @@ import net.minecraft.core.QuartPos;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.DistanceManager;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.Ticket;
import net.minecraft.server.level.TicketType;
import net.minecraft.tags.TagKey;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.chunk.PalettedContainer;
@@ -59,7 +56,6 @@ import net.minecraft.world.level.levelgen.RandomState;
import net.minecraft.world.level.levelgen.structure.Structure;
import net.minecraft.world.level.levelgen.structure.StructureType;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
@@ -82,10 +78,6 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.protocol.game.ClientboundExplodePacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.Ticket;
import net.minecraft.server.level.TicketType;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.util.SortedArraySet;
@@ -135,6 +127,8 @@ import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.storage.ServerLevelData;
import net.minecraft.world.phys.Vec3;
import javax.annotation.Nullable;
import static carpet.script.utils.WorldTools.canHasChunk;
public class WorldAccess
@@ -256,7 +250,7 @@ public class WorldAccess
{
synchronized (level)
{
((ChunkGeneratorInterface) level.getChunkSource().getGenerator()).initStrongholds(level);
level.getChunkSource().getGeneratorState().ensureStructuresGenerated();
}
}
@@ -444,7 +438,7 @@ public class WorldAccess
}
final Value weather = lv.get(0);
final ServerLevelData worldProperties = ((ServerWorldInterface) world).getWorldPropertiesCM();
final ServerLevelData worldProperties = Vanilla.ServerLevel_getWorldProperties(world);
if (lv.size() == 1)
{
return new NumericValue(switch (weather.getString().toLowerCase(Locale.ROOT))
@@ -644,10 +638,9 @@ public class WorldAccess
expression.addContextFunction("chunk_tickets", -1, (c, t, lv) ->
{
final ServerLevel world = ((CarpetContext) c).level();
final Long2ObjectOpenHashMap<SortedArraySet<Ticket<?>>> levelTickets = (
(ChunkTicketManagerInterface) ((ServerChunkManagerInterface) world.getChunkSource())
.getCMTicketManager()
).getTicketsByPosition();
DistanceManager foo = Vanilla.ServerChunkCache_getCMTicketManager(world.getChunkSource());
final Long2ObjectOpenHashMap<SortedArraySet<Ticket<?>>> levelTickets = Vanilla.ChunkTicketManager_getTicketsByPosition(foo);
final List<Value> res = new ArrayList<>();
if (lv.size() == 0)
{
@@ -723,22 +716,23 @@ public class WorldAccess
// lazy cause its parked execution
expression.addLazyFunction("without_updates", 1, (c, t, lv) ->
{
final boolean previous = CarpetSettings.impendingFillSkipUpdates.get();
if (previous)
if (Carpet.getImpendingFillSkipUpdates().get())
{
return lv.get(0);
}
final Value[] result = new Value[]{Value.NULL};
((CarpetContext) c).server().executeBlocking(() ->
{
final ThreadLocal<Boolean> skipUpdates = Carpet.getImpendingFillSkipUpdates();
final boolean previous = skipUpdates.get();
try
{
CarpetSettings.impendingFillSkipUpdates.set(true);
skipUpdates.set(true);
result[0] = lv.get(0).evalValue(c, t);
}
finally
{
CarpetSettings.impendingFillSkipUpdates.set(false);
skipUpdates.set(previous);
}
});
return (cc, tt) -> result[0];
@@ -1080,7 +1074,9 @@ public class WorldAccess
final Explosion explosion = new Explosion(cc.level(), source, null, null, pos.x, pos.y, pos.z, powah, createFire, mode)
{
@Override
public @Nullable LivingEntity getIndirectSourceEntity()
@Nullable
public
LivingEntity getIndirectSourceEntity()
{
return theAttacker;
}
@@ -1168,15 +1164,15 @@ public class WorldAccess
expression.addContextFunction("block_sound", -1, (c, t, lv) ->
stateStringQuery(c, "block_sound", lv, (s, p) ->
BlockInfo.soundName.get(s.getSoundType())));
Carpet.getSoundTypeNames().get(s.getSoundType())));
expression.addContextFunction("material", -1, (c, t, lv) ->
stateStringQuery(c, "material", lv, (s, p) ->
BlockInfo.materialName.get(s.getMaterial())));
Carpet.getMaterialNames().get(s.getMaterial())));
expression.addContextFunction("map_colour", -1, (c, t, lv) ->
stateStringQuery(c, "map_colour", lv, (s, p) ->
BlockInfo.mapColourName.get(s.getMapColor(((CarpetContext) c).level(), p))));
Carpet.getMapColorNames().get(s.getMapColor(((CarpetContext) c).level(), p))));
// Deprecated for block_state()
@@ -1485,7 +1481,7 @@ public class WorldAccess
}
catch (final NullPointerException npe)
{
CarpetSettings.LOG.error("Failed to detect structure: " + reg.getKey(str));
CarpetScriptServer.LOG.error("Failed to detect structure: " + reg.getKey(str));
start = null;
}
@@ -1649,7 +1645,7 @@ public class WorldAccess
final Value[] result = new Value[]{Value.NULL};
((CarpetContext) c).server().executeBlocking(() ->
{
final Map<String, Integer> report = ((ThreadedAnvilChunkStorageInterface) world.getChunkSource().chunkMap).regenerateChunkRegion(requestedChunks);
final Map<String, Integer> report = Vanilla.ChunkMap_regenerateChunkRegion(world.getChunkSource().chunkMap, requestedChunks);
result[0] = MapValue.wrap(report.entrySet().stream().collect(Collectors.toMap(
e -> new StringValue(e.getKey()),
e -> new NumericValue(e.getValue())
@@ -1677,9 +1673,7 @@ public class WorldAccess
required_charge = NumericValue.asNumber(lv.get(locator.offset)).getDouble();
}
final NaturalSpawner.SpawnState charger = cc.level().getChunkSource().getLastSpawnState();
return charger == null ? Value.NULL : new NumericValue(
((SpawnHelperInnerInterface) charger).getPotentialCalculator().
getPotentialEnergyChange(pos, required_charge)
return charger == null ? Value.NULL : new NumericValue(Vanilla.SpawnState_getPotentialCalculator(charger).getPotentialEnergyChange(pos, required_charge)
);
});
@@ -1809,7 +1803,7 @@ public class WorldAccess
noiseBasedChunkGenerator.generatorSettings().value(),
level.registryAccess().lookupOrThrow(Registries.NOISE), level.getSeed()
);
DensityFunction.Visitor visitor = ((RandomStateVisitorAccessor) (Object) randomState).getVisitor();
DensityFunction.Visitor visitor = Vanilla.RandomState_getVisitor(randomState);
return densityFunction.mapAll(visitor);
}