24w40a
This commit is contained in:
1 parent
6144967299
commit
3a66dd0a9b
17 files changed
+269
-232
No files matched your search
@@ -6,8 +6,8 @@ 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.utils.RecipeHelper;
|
||||
import carpet.script.value.BooleanValue;
|
||||
import carpet.script.value.EntityValue;
|
||||
import carpet.script.value.FormattedTextValue;
|
||||
@@ -49,6 +49,7 @@ import net.minecraft.world.item.crafting.RecipeType;
|
||||
import net.minecraft.world.item.crafting.ShapedRecipe;
|
||||
import net.minecraft.world.item.crafting.ShapelessRecipe;
|
||||
import net.minecraft.world.item.crafting.SingleItemRecipe;
|
||||
import net.minecraft.world.item.crafting.display.SlotDisplay;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
public class Inventories
|
||||
@@ -103,7 +104,7 @@ public class Inventories
|
||||
throw new InternalExpressionException("'recipe_data' requires at least one argument");
|
||||
}
|
||||
String recipeName = lv.get(0).getString();
|
||||
RecipeType<?> type = RecipeType.CRAFTING;
|
||||
RecipeType<? extends Recipe<?>> type = RecipeType.CRAFTING;
|
||||
if (lv.size() > 1)
|
||||
{
|
||||
String recipeType = lv.get(1).getString();
|
||||
@@ -113,30 +114,44 @@ public class Inventories
|
||||
throw new InternalExpressionException("Unknown recipe type: " + recipeType);
|
||||
}
|
||||
}
|
||||
List<Recipe<?>> recipes = Vanilla.RecipeManager_getAllMatching(cc.server().getRecipeManager(), type, InputValidator.identifierOf(recipeName), cc.registryAccess());
|
||||
List<Recipe<?>> recipes = RecipeHelper.getRecipesForOutput(cc.server().getRecipeManager(), type, InputValidator.identifierOf(recipeName), cc.level());
|
||||
if (recipes.isEmpty())
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
List<Value> recipesOutput = new ArrayList<>();
|
||||
RegistryAccess regs = cc.registryAccess();
|
||||
SlotDisplay.ResolutionContext context = SlotDisplay.ResolutionContext.forLevel(cc.level());
|
||||
for (Recipe<?> recipe : recipes)
|
||||
{
|
||||
ItemStack result = recipe.getResultItem(regs);
|
||||
List<Value> results = new ArrayList<>();
|
||||
//ItemStack result = recipe.display().forEach(); getResultItem(regs);
|
||||
recipe.display().forEach(rd -> rd.result().resolveForStacks(context).forEach(is -> results.add(ValueConversions.of(is, regs))));
|
||||
|
||||
|
||||
List<Value> ingredientValue = new ArrayList<>();
|
||||
recipe.placementInfo().slotInfo().forEach(ingredient -> {
|
||||
if (ingredient.isEmpty())
|
||||
for (Optional<PlacementInfo.SlotInfo> info : recipe.placementInfo().slotInfo())
|
||||
{
|
||||
if (info.isEmpty())
|
||||
{
|
||||
ingredientValue.add(Value.NULL);
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
PlacementInfo.SlotInfo value = ingredient.get();
|
||||
int position = value.placerOutputPosition();
|
||||
PlacementInfo.SlotInfo value = info.get();
|
||||
int ingredientIndex = value.placerOutputPosition();
|
||||
|
||||
List<Value> alternatives = new ArrayList<>();
|
||||
value.possibleItems().stream().map(is -> ValueConversions.of(is, regs)).forEach(alternatives::add);
|
||||
ingredientValue.add(ListValue.wrap(alternatives));
|
||||
});
|
||||
recipe.placementInfo().ingredients().get(ingredientIndex).items().forEach(item -> alternatives.add(ValueConversions.of(item.value(), regs)));
|
||||
if (alternatives.isEmpty())
|
||||
{
|
||||
ingredientValue.add(Value.NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
ingredientValue.add(ListValue.wrap(alternatives));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Value recipeSpec;
|
||||
if (recipe instanceof ShapedRecipe shapedRecipe)
|
||||
@@ -155,8 +170,8 @@ public class Inventories
|
||||
{
|
||||
recipeSpec = ListValue.of(
|
||||
new StringValue("smelting"),
|
||||
new NumericValue(abstractCookingRecipe.getCookingTime()),
|
||||
new NumericValue(abstractCookingRecipe.getExperience())
|
||||
new NumericValue(abstractCookingRecipe.cookingTime()),
|
||||
new NumericValue(abstractCookingRecipe.experience())
|
||||
);
|
||||
}
|
||||
else if (recipe instanceof SingleItemRecipe)
|
||||
@@ -172,7 +187,7 @@ public class Inventories
|
||||
recipeSpec = ListValue.of(new StringValue("custom"));
|
||||
}
|
||||
|
||||
recipesOutput.add(ListValue.of(ValueConversions.of(result, regs), ListValue.wrap(ingredientValue), recipeSpec));
|
||||
recipesOutput.add(ListValue.of(ListValue.wrap(results), ListValue.wrap(ingredientValue), recipeSpec));
|
||||
}
|
||||
return ListValue.wrap(recipesOutput);
|
||||
});
|
||||
@@ -181,10 +196,11 @@ public class Inventories
|
||||
{
|
||||
String itemStr = v.get(0).getString();
|
||||
ResourceLocation id = InputValidator.identifierOf(itemStr);
|
||||
Registry<Item> registry = ((CarpetContext) c).registry(Registries.ITEM);
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
Registry<Item> registry = cc.registry(Registries.ITEM);
|
||||
Item item = registry.getOptional(id).orElseThrow(() -> new ThrowStatement(itemStr, Throwables.UNKNOWN_ITEM));
|
||||
Item reminder = item.getCraftingRemainingItem();
|
||||
return reminder == null ? Value.NULL : NBTSerializableValue.nameFromRegistryId(registry.getKey(reminder));
|
||||
ItemStack reminder = item.getCraftingRemainder();
|
||||
return reminder.isEmpty() ? Value.NULL : ValueConversions.of(reminder, cc.registryAccess());
|
||||
});
|
||||
|
||||
expression.addContextFunction("inventory_size", -1, (c, t, lv) ->
|
||||
|
||||
@@ -55,7 +55,6 @@ import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.advancements.AdvancementHolder;
|
||||
import net.minecraft.advancements.critereon.MinMaxBounds;
|
||||
import net.minecraft.commands.CommandBuildContext;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
@@ -204,12 +203,8 @@ public abstract class CommandArgument
|
||||
|
||||
// resource / identifier section
|
||||
|
||||
new VanillaUnconfigurableArgument("recipe", ResourceLocationArgument::id,
|
||||
(c, p) -> ValueConversions.of(ResourceLocationArgument.getRecipe(c, p).id()), SuggestionProviders.ALL_RECIPES
|
||||
),
|
||||
new VanillaUnconfigurableArgument("advancement", ResourceLocationArgument::id,
|
||||
(c, p) -> ValueConversions.of(ResourceLocationArgument.getAdvancement(c, p).id()), (ctx, builder) -> SharedSuggestionProvider.suggestResource(ctx.getSource().getServer().getAdvancements().getAllAdvancements().stream().map(AdvancementHolder::id), builder)
|
||||
),
|
||||
new VanillaUnconfigurableArgument("recipe", Registries.RECIPE),
|
||||
new VanillaUnconfigurableArgument("advancement", Registries.ADVANCEMENT),
|
||||
new VanillaUnconfigurableArgument("lootcondition", ResourceLocationArgument::id,
|
||||
(c, p) -> ValueConversions.of(ResourceLocationArgument.getId(c, p)), (ctx, builder) -> SharedSuggestionProvider.suggestResource(ctx.getSource().getServer().reloadableRegistries().getKeys(Registries.LOOT_CONDITION_TYPE), builder)
|
||||
),
|
||||
|
||||
@@ -13,13 +13,11 @@ import carpet.fakes.LivingEntityInterface;
|
||||
import carpet.fakes.MinecraftServerInterface;
|
||||
import carpet.fakes.MobEntityInterface;
|
||||
import carpet.fakes.RandomStateVisitorAccessor;
|
||||
import carpet.fakes.RecipeManagerInterface;
|
||||
import carpet.fakes.AbstractContainerMenuInterface;
|
||||
import carpet.fakes.ServerPlayerInterface;
|
||||
import carpet.fakes.ServerPlayerInteractionManagerInterface;
|
||||
import carpet.fakes.ServerWorldInterface;
|
||||
import carpet.fakes.SpawnHelperInnerInterface;
|
||||
import carpet.fakes.ThreadedAnvilChunkStorageInterface;
|
||||
import carpet.mixins.Objective_scarpetMixin;
|
||||
import carpet.mixins.PoiRecord_scarpetMixin;
|
||||
import carpet.mixins.Scoreboard_scarpetMixin;
|
||||
@@ -38,10 +36,8 @@ import net.fabricmc.loader.api.ModContainer;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.arguments.blocks.BlockInput;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.RegistryAccess;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ChunkMap;
|
||||
import net.minecraft.server.level.DistanceManager;
|
||||
@@ -62,11 +58,6 @@ import net.minecraft.world.entity.animal.horse.AbstractHorse;
|
||||
import net.minecraft.world.entity.item.ItemEntity;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraft.world.inventory.DataSlot;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.item.crafting.Recipe;
|
||||
import net.minecraft.world.item.crafting.RecipeManager;
|
||||
import net.minecraft.world.item.crafting.RecipeType;
|
||||
import net.minecraft.world.level.ChunkPos;
|
||||
import net.minecraft.world.level.GameRules;
|
||||
import net.minecraft.world.level.NaturalSpawner;
|
||||
@@ -83,7 +74,6 @@ import net.minecraft.world.scores.Objective;
|
||||
import net.minecraft.world.scores.Scoreboard;
|
||||
import net.minecraft.world.scores.criteria.ObjectiveCriteria;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -107,11 +97,6 @@ public class Vanilla
|
||||
return Map.of(); //return ((ThreadedAnvilChunkStorageInterface) chunkMap).regenerateChunkRegion(requestedChunks);
|
||||
}
|
||||
|
||||
public static List<Recipe<?>> RecipeManager_getAllMatching(RecipeManager recipeManager, RecipeType<?> type, ResourceLocation output, RegistryAccess registryAccess)
|
||||
{
|
||||
return ((RecipeManagerInterface) recipeManager).getAllMatching(type, output, registryAccess);
|
||||
}
|
||||
|
||||
public static int NaturalSpawner_MAGIC_NUMBER()
|
||||
{
|
||||
return SpawnReporter.MAGIC_NUMBER;
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package carpet.script.utils;
|
||||
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.item.crafting.Recipe;
|
||||
import net.minecraft.world.item.crafting.RecipeManager;
|
||||
import net.minecraft.world.item.crafting.RecipeType;
|
||||
import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
||||
import net.minecraft.world.item.crafting.display.SlotDisplay;
|
||||
import net.minecraft.world.level.Level;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RecipeHelper
|
||||
{
|
||||
public static List<Recipe<?>> getRecipesForOutput(RecipeManager recipeManager, RecipeType<?> type, ResourceLocation id, Level level)
|
||||
{
|
||||
List<Recipe<?>> results = new ArrayList<>();
|
||||
SlotDisplay.ResolutionContext context = SlotDisplay.ResolutionContext.forLevel(level);
|
||||
recipeManager.getRecipes().forEach(r -> {
|
||||
if (r.value().getType() == type)
|
||||
{
|
||||
for (RecipeDisplay recipeDisplay : r.value().display())
|
||||
{
|
||||
recipeDisplay.result().resolveForStacks(context).forEach(stack -> {
|
||||
if (BuiltInRegistries.ITEM.wrapAsHolder(stack.getItem()).unwrapKey().map(ResourceKey::location).orElseThrow(IllegalStateException::new).equals(id))
|
||||
{
|
||||
results.add(r.value());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
public static List<Recipe<?>> getRecipesForOutput(RecipeManager recipeManager, ResourceLocation id, Level level)
|
||||
{
|
||||
List<Recipe<?>> results = new ArrayList<>();
|
||||
SlotDisplay.ResolutionContext context = SlotDisplay.ResolutionContext.forLevel(level);
|
||||
recipeManager.getRecipes().forEach(r -> {
|
||||
for (RecipeDisplay recipeDisplay : r.value().display())
|
||||
{
|
||||
recipeDisplay.result().resolveForStacks(context).forEach(stack -> {
|
||||
if (BuiltInRegistries.ITEM.wrapAsHolder(stack.getItem()).unwrapKey().map(ResourceKey::location).orElseThrow(IllegalStateException::new).equals(id))
|
||||
{
|
||||
results.add(r.value());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
return results;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,8 @@ import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.StringTag;
|
||||
import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetExperiencePacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetHeldSlotPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
@@ -922,7 +924,7 @@ public class EntityValue extends Value
|
||||
}
|
||||
else
|
||||
{
|
||||
((ServerLevel) e.getCommandSenderWorld()).getChunkSource().broadcastAndSend(e, new ClientboundTeleportEntityPacket(e));
|
||||
((ServerLevel) e.getCommandSenderWorld()).getChunkSource().broadcastAndSend(e, ClientboundEntityPositionSyncPacket.of(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.ai.behavior.PositionTracker;
|
||||
import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
|
||||
import net.minecraft.world.entity.ai.memory.WalkTarget;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
@@ -99,12 +100,17 @@ public class ValueConversions
|
||||
public static Value of(ItemStack stack, RegistryAccess regs)
|
||||
{
|
||||
return stack == null || stack.isEmpty() ? Value.NULL : ListValue.of(
|
||||
of(regs.lookupOrThrow(Registries.ITEM).getKey(stack.getItem())),
|
||||
of(stack.getItem(), regs),
|
||||
new NumericValue(stack.getCount()),
|
||||
NBTSerializableValue.fromStack(stack, regs)
|
||||
);
|
||||
}
|
||||
|
||||
public static Value of(Item item, RegistryAccess regs)
|
||||
{
|
||||
return of(regs.lookupOrThrow(Registries.ITEM).getKey(item));
|
||||
}
|
||||
|
||||
public static Value of(Objective objective)
|
||||
{
|
||||
return ListValue.of(
|
||||
|
||||
Reference in new issue
Block a user