Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 9 additions & 18 deletions src/main/generated/data/minecraft/item/compass.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,20 @@
"type": "minecraft:sequence",
"handler": "minecraft:passing",
"entries": [
{
"action": {
"type": "minecraft:modify_item",
"item_modifier": {
"count": 1.0,
"function": "minecraft:split"
}
}
},
{
"action": {
"type": "minecraft:set_item_pointer_location",
"position": "interacted",
"stack": "result"
}
},
{
"action": {
"type": "minecraft:modify_item",
"item_modifier": {
"function": "minecraft:sequence",
"functions": [
{
"count": 1.0,
"function": "minecraft:split"
},
{
"function": "minecraft:set_item_pointer_location",
"position": "interacted"
},
{
"function": "minecraft:set_name",
"name": {
Expand All @@ -48,8 +40,7 @@
"function": "minecraft:set_components"
}
]
},
"stack": "result"
}
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import net.errorcraft.itematic.item.event.ItemEvent;
import net.errorcraft.itematic.world.action.context.ActionContext;
import net.minecraft.component.MergedComponentMap;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

Expand All @@ -24,6 +26,9 @@ public interface ItemStackAccess {
default int itematic$tryDecrement(int amount) {
return 0;
}
default ItemStack itematic$copyOrSplit(@Nullable LivingEntity holder, int amount) {
return ItemStack.EMPTY;
}
default ItemStack itematic$copyWithItem(RegistryEntry<Item> item) {
return ItemStack.EMPTY;
}
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/net/errorcraft/itematic/item/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.errorcraft.itematic.item.shooter.method.methods.DirectShooterMethod;
import net.errorcraft.itematic.item.smithing.template.SmithingTemplates;
import net.errorcraft.itematic.loot.condition.LocationCheckLootConditionUtil;
import net.errorcraft.itematic.loot.function.SetItemPointerLocationItemModifier;
import net.errorcraft.itematic.loot.function.SplitItemModifier;
import net.errorcraft.itematic.loot.predicate.SideCheckPredicate;
import net.errorcraft.itematic.mixin.item.BrushItemAccessor;
Expand Down Expand Up @@ -10991,14 +10992,8 @@ private void bootstrapMiscellaneous() {
PassingSequenceHandler.builder()
.add(ModifyItemAction.of(
ItemStackTarget.TOOL,
SplitItemModifier.builder(1)
))
.add(SetItemPointerLocationAction.of(
ItemStackTarget.RESULT,
PositionTarget.INTERACTED_POSITION
))
.add(ModifyItemAction.of(
ItemStackTarget.RESULT,
SplitItemModifier.builder(1),
SetItemPointerLocationItemModifier.builder(PositionTarget.INTERACTED_POSITION),
SetNameLootFunction.builder(
Text.translatable(Util.createTranslationKey("item", Identifier.ofVanilla("lodestone_compass"))),
SetNameLootFunction.Target.ITEM_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ItematicItemModifierTypes {
public static final LootFunctionType<DyeItemModifier> DYE = LootFunctionTypesAccessor.register("dye", DyeItemModifier.CODEC);
public static final LootFunctionType<SetRandomPotionItemModifier> SET_RANDOM_POTION = LootFunctionTypesAccessor.register("set_random_potion", SetRandomPotionItemModifier.CODEC);
public static final LootFunctionType<SplitItemModifier> SPLIT = LootFunctionTypesAccessor.register("split", SplitItemModifier.CODEC);
public static final LootFunctionType<SetItemPointerLocationItemModifier> SET_ITEM_POINTER_LOCATION = LootFunctionTypesAccessor.register("set_item_pointer_location", SetItemPointerLocationItemModifier.CODEC);

private ItematicItemModifierTypes() {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.errorcraft.itematic.loot.function;

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.errorcraft.itematic.world.action.context.PositionTarget;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.LodestoneTrackerComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.condition.LootCondition;
import net.minecraft.loot.context.LootContext;
import net.minecraft.loot.function.ConditionalLootFunction;
import net.minecraft.loot.function.LootFunctionType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.GlobalPos;
import net.minecraft.util.math.Vec3d;

import java.util.List;
import java.util.Optional;

public class SetItemPointerLocationItemModifier extends ConditionalLootFunction {
public static final MapCodec<SetItemPointerLocationItemModifier> CODEC = RecordCodecBuilder.mapCodec(instance -> addConditionsField(instance).and(
PositionTarget.CODEC.fieldOf("position").forGetter(split -> split.position)
).apply(instance, SetItemPointerLocationItemModifier::new));

private final PositionTarget position;

public SetItemPointerLocationItemModifier(PositionTarget position) {
this(List.of(), position);
}

public SetItemPointerLocationItemModifier(List<LootCondition> conditions, PositionTarget position) {
super(conditions);
this.position = position;
}

public static Builder<?> builder(PositionTarget position) {
return builder(conditions -> new SetItemPointerLocationItemModifier(conditions, position));
}

@Override
public LootFunctionType<SetItemPointerLocationItemModifier> getType() {
return ItematicItemModifierTypes.SET_ITEM_POINTER_LOCATION;
}

@Override
protected ItemStack process(ItemStack stack, LootContext context) {
Vec3d pos = context.get(this.position.parameter());
if (pos == null) {
return stack;
}

stack.set(
DataComponentTypes.LODESTONE_TRACKER,
new LodestoneTrackerComponent(
Optional.of(GlobalPos.create(
context.getWorld().getRegistryKey(),
BlockPos.ofFloored(pos)
)),
true
)
);
return stack;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.condition.LootCondition;
import net.minecraft.loot.context.LootContext;
import net.minecraft.loot.context.LootContextParameters;
import net.minecraft.loot.function.ConditionalLootFunction;
import net.minecraft.loot.function.LootFunctionType;
import net.minecraft.loot.provider.number.ConstantLootNumberProvider;
Expand All @@ -30,7 +32,7 @@ public SplitItemModifier(List<LootCondition> conditions, LootNumberProvider coun
}

public static Builder<?> builder(int count) {
return builder(conditions -> new SplitItemModifier(ConstantLootNumberProvider.create(count)));
return builder(conditions -> new SplitItemModifier(conditions, ConstantLootNumberProvider.create(count)));
}

@Override
Expand All @@ -40,6 +42,7 @@ public LootFunctionType<SplitItemModifier> getType() {

@Override
protected ItemStack process(ItemStack stack, LootContext context) {
return stack.split(this.count.nextInt(context));
LivingEntity holder = context.get(LootContextParameters.THIS_ENTITY) instanceof LivingEntity target ? target : null;
return stack.itematic$copyOrSplit(holder, this.count.nextInt(context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public abstract class ItemStackExtender implements ComponentHolder, ItemStackAcc
@Shadow
public abstract boolean isEmpty();

@Shadow
public abstract ItemStack split(int amount);

@Shadow
public abstract void damage(int amount, ServerWorld world, @Nullable ServerPlayerEntity player, Consumer<Item> breakCallback);

Expand All @@ -103,6 +106,9 @@ public abstract class ItemStackExtender implements ComponentHolder, ItemStackAcc
@Shadow
public abstract int getMaxCount();

@Shadow
public abstract ItemStack copyWithCount(int count);

@Shadow
public abstract int getCount();

Expand Down Expand Up @@ -707,6 +713,15 @@ public boolean canBeEnchantedWith(RegistryEntry<Enchantment> enchantment, Enchan
return actualAmount;
}

@Override
public ItemStack itematic$copyOrSplit(@Nullable LivingEntity holder, int amount) {
if (holder != null && holder.isInCreativeMode()) {
return this.copyWithCount(amount);
}

return this.split(amount);
}

@Override
public ItemStack itematic$copyWithItem(RegistryEntry<Item> item) {
return this.itematic$copyComponentsToNewStack(item, this.count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class ActionTypeKeys {
public static final RegistryKey<ActionType<?>> MODIFY_SIGN = of("modify_sign");
public static final RegistryKey<ActionType<?>> WAX_BLOCK = of("wax_block");
public static final RegistryKey<ActionType<?>> DECREMENT_ITEM = of("decrement_item");
public static final RegistryKey<ActionType<?>> SET_ITEM_POINTER_LOCATION = of("set_item_pointer_location");
public static final RegistryKey<ActionType<?>> LIGHT_END_PORTAL = of("light_end_portal");
public static final RegistryKey<ActionType<?>> PLAY_SOUND = of("play_sound");
public static final RegistryKey<ActionType<?>> DISPLAY_PARTICLE = of("display_particle");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class ActionTypes {
public static final ActionType<ModifySignAction> MODIFY_SIGN = register(ActionTypeKeys.MODIFY_SIGN, new ActionType<>(ModifySignAction.CODEC));
public static final ActionType<WaxBlockAction> WAX_BLOCK = register(ActionTypeKeys.WAX_BLOCK, new ActionType<>(WaxBlockAction.CODEC));
public static final ActionType<DecrementItemAction> DECREMENT_ITEM = register(ActionTypeKeys.DECREMENT_ITEM, new ActionType<>(DecrementItemAction.CODEC));
public static final ActionType<SetItemPointerLocationAction> SET_ITEM_POINTER_LOCATION = register(ActionTypeKeys.SET_ITEM_POINTER_LOCATION, new ActionType<>(SetItemPointerLocationAction.CODEC));
public static final ActionType<LightEndPortalAction> LIGHT_END_PORTAL = register(ActionTypeKeys.LIGHT_END_PORTAL, new ActionType<>(LightEndPortalAction.CODEC));
public static final ActionType<PlaySoundAction> PLAY_SOUND = register(ActionTypeKeys.PLAY_SOUND, new ActionType<>(PlaySoundAction.CODEC));
public static final ActionType<DisplayParticleAction> DISPLAY_PARTICLE = register(ActionTypeKeys.DISPLAY_PARTICLE, new ActionType<>(DisplayParticleAction.CODEC));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ActionType<ModifyItemAction> type() {

@Override
public boolean execute(ActionContext context) {
ItemStack stack = this.stack.get(context);
ItemStack stack = context.get(this.stack.parameter());
if (ItemStackUtil.isNullOrEmpty(stack)) {
return false;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,27 @@
import net.minecraft.item.ItemStack;
import net.minecraft.loot.context.LootContextParameters;
import net.minecraft.util.StringIdentifiable;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;
import net.minecraft.util.context.ContextParameter;

public enum ItemStackTarget implements StringIdentifiable {
TOOL("tool", context -> context.get(LootContextParameters.TOOL)),
RESULT("result", ActionContext::resultStack);
TOOL("tool", LootContextParameters.TOOL);

public static final Codec<ItemStackTarget> CODEC = StringIdentifiable.createCodec(ItemStackTarget::values);

private final String name;
private final Function<ActionContext, ItemStack> itemStackSupplier;
private final ContextParameter<ItemStack> parameter;

ItemStackTarget(String name, Function<ActionContext, ItemStack> itemStackSupplier) {
ItemStackTarget(String name, ContextParameter<ItemStack> parameter) {
this.name = name;
this.itemStackSupplier = itemStackSupplier;
this.parameter = parameter;
}

@Override
public String asString() {
return this.name;
}

@Nullable
public ItemStack get(ActionContext context) {
return this.itemStackSupplier.apply(context);
public ContextParameter<ItemStack> parameter() {
return this.parameter;
}
}
Loading