diff --git a/src/main/java/com/minelittlepony/unicopia/item/UItems.java b/src/main/java/com/minelittlepony/unicopia/item/UItems.java index ae86f9ca..3d4f6429 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/UItems.java +++ b/src/main/java/com/minelittlepony/unicopia/item/UItems.java @@ -1,12 +1,18 @@ package com.minelittlepony.unicopia.item; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + import com.minelittlepony.unicopia.USounds; +import com.minelittlepony.unicopia.item.toxin.ToxicHolder; import com.minelittlepony.unicopia.item.toxin.Toxics; import com.minelittlepony.unicopia.item.toxin.UFoodComponents; import net.minecraft.item.Item; import net.minecraft.item.Item.Settings; import net.minecraft.item.ItemGroup; +import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; import net.minecraft.item.BlockItem; import net.minecraft.item.FoodComponents; import net.minecraft.item.MusicDiscItem; @@ -17,6 +23,8 @@ import net.minecraft.util.registry.Registry; public interface UItems { + List ITEMS = new ArrayList<>(); + AppleItem GREEN_APPLE = register("green_apple", new AppleItem(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE))); AppleItem SWEET_APPLE = register("sweet_apple", new AppleItem(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE))); AppleItem SOUR_APPLE = register("sour_apple", new AppleItem(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE))); @@ -32,6 +40,7 @@ public interface UItems { Item MUSIC_DISC_FUNK = register("music_disc_funk", USounds.RECORD_FUNK); static T register(String name, T item) { + ITEMS.add(item); if (item instanceof BlockItem) { ((BlockItem)item).appendBlocks(Item.BLOCK_ITEMS, item); } @@ -48,5 +57,17 @@ public interface UItems { static void bootstrap() { Toxics.bootstrap(); + + FabricItemGroupBuilder.create(new Identifier("unicopia", "items")).appendItems(list -> { + list.addAll(VanillaOverrides.REGISTRY.stream().map(Item::getStackForRender).collect(Collectors.toList())); + list.addAll(ITEMS.stream().map(Item::getStackForRender).collect(Collectors.toList())); + }).icon(ZAP_APPLE::getStackForRender).build(); + + FabricItemGroupBuilder.create(new Identifier("unicopia", "horsefeed")).appendItems(list -> { + list.addAll(Registry.ITEM.stream() + .filter(item -> item instanceof ToxicHolder && ((ToxicHolder)item).getToxic().isPresent()) + .map(Item::getStackForRender) + .collect(Collectors.toList())); + }).icon(ZAP_APPLE::getStackForRender).build(); } } diff --git a/src/main/java/com/minelittlepony/unicopia/item/VanillaOverrides.java b/src/main/java/com/minelittlepony/unicopia/item/VanillaOverrides.java index ea00c3cd..e867b953 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/VanillaOverrides.java +++ b/src/main/java/com/minelittlepony/unicopia/item/VanillaOverrides.java @@ -19,4 +19,4 @@ public final class VanillaOverrides { static { register("apple", new AppleItem(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE))); } -} \ No newline at end of file +} diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxic.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxic.java index 6a28e5b8..8a371d06 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxic.java +++ b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxic.java @@ -8,7 +8,6 @@ import com.minelittlepony.unicopia.entity.player.Pony; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; @@ -19,18 +18,15 @@ import net.minecraft.world.World; public class Toxic { - private final Item item; - private final UseAction action; private final Function toxicity; private final Toxin toxin; - Toxic(Item item, UseAction action, Toxin toxin, Toxicity toxicity) { - this(item, action, toxin, stack -> toxicity); + Toxic(UseAction action, Toxin toxin, Toxicity toxicity) { + this(action, toxin, stack -> toxicity); } - Toxic(Item item, UseAction action, Toxin toxin, Function toxicity) { - this.item = item; + Toxic(UseAction action, Toxin toxin, Function toxicity) { this.action = action; this.toxin = toxin; this.toxicity = toxicity; @@ -52,7 +48,11 @@ public class Toxic { toxin.afflict((PlayerEntity)entity, t, stack); } - return new ItemStack(item.getRecipeRemainder()); + if (!(entity instanceof PlayerEntity) || !((PlayerEntity)entity).abilities.creativeMode) { + stack.decrement(1); + } + + return stack; } public TypedActionResult use(World world, PlayerEntity player, Hand hand, Supplier> sup) { diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicHolder.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicHolder.java index ac5e37e2..b788678c 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicHolder.java +++ b/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicHolder.java @@ -1,8 +1,15 @@ package com.minelittlepony.unicopia.item.toxin; +import java.util.Optional; + import net.minecraft.item.FoodComponent; public interface ToxicHolder { void setFood(FoodComponent food); + void setToxic(Toxic toxic); + + default Optional getToxic() { + return Optional.empty(); + } } diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicItem.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicItem.java deleted file mode 100644 index d427cc46..00000000 --- a/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicItem.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.minelittlepony.unicopia.item.toxin; - -import java.util.List; -import java.util.function.Function; - -import javax.annotation.Nullable; - -import net.minecraft.client.item.TooltipContext; -import net.minecraft.entity.LivingEntity; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.text.Text; -import net.minecraft.util.Hand; -import net.minecraft.util.TypedActionResult; -import net.minecraft.util.UseAction; -import net.minecraft.world.World; - -public class ToxicItem extends Item { - - private final Toxic toxic; - - public ToxicItem(Item.Settings settings, UseAction action, Toxicity toxicity, Toxin toxin) { - this(settings, action, stack -> toxicity, toxin); - } - - public ToxicItem(Item.Settings settings, UseAction action, Function toxicity, Toxin toxin) { - super(settings); - this.toxic = new Toxic(this, action, toxin, toxicity); - } - - @Override - public UseAction getUseAction(ItemStack stack) { - return toxic.getUseAction(stack); - } - - @Override - public void appendTooltip(ItemStack stack, @Nullable World world, List tooltip, TooltipContext context) { - tooltip.add(toxic.getTooltip(stack)); - } - - @Override - public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) { - super.finishUsing(stack, world, entity); - return toxic.finishUsing(stack, world, entity); - } - - @Override - public TypedActionResult use(World world, PlayerEntity player, Hand hand) { - return toxic.use(world, player, hand, () -> super.use(world, player, hand)); - } -} diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java index 183268b7..8d7b648e 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java +++ b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java @@ -31,7 +31,7 @@ public interface Toxics { static void bootstrap() {} static Toxic register(Item target, FoodComponent food, UseAction action, Toxicity toxicity, Toxin toxin) { - Toxic toxic = new Toxic(target, action, toxin, toxicity); + Toxic toxic = new Toxic(action, toxin, toxicity); ToxicHolder holder = (ToxicHolder)target; holder.setFood(food); holder.setToxic(toxic); diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/MixinBlockItem.java b/src/main/java/com/minelittlepony/unicopia/mixin/MixinBlockItem.java index 7a3eac5e..5f4a335e 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/MixinBlockItem.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/MixinBlockItem.java @@ -1,6 +1,7 @@ package com.minelittlepony.unicopia.mixin; import java.util.List; +import java.util.Optional; import javax.annotation.Nullable; @@ -28,43 +29,47 @@ import net.minecraft.world.World; abstract class MixinBlockItem extends Item implements ToxicHolder { public MixinBlockItem() {super(null); } - @Nullable - private Toxic toxic; + private Optional toxic = Optional.empty(); @Override public void setToxic(Toxic toxic) { - this.toxic = toxic; + this.toxic = Optional.of(toxic); + } + + @Override + public Optional getToxic() { + return toxic; } @Override public UseAction getUseAction(ItemStack stack) { - if (toxic != null) { - return toxic.getUseAction(stack); + if (toxic.isPresent()) { + return toxic.get().getUseAction(stack); } return super.getUseAction(stack); } @Inject(method = "appendTooltip", at = @At("RETURN")) private void onAppendTooltip(ItemStack stack, @Nullable World world, List tooltip, TooltipContext context, CallbackInfo into) { - if (toxic != null) { - tooltip.add(toxic.getTooltip(stack)); + if (toxic.isPresent()) { + tooltip.add(toxic.get().getTooltip(stack)); } } @Override public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) { ItemStack result = super.finishUsing(stack, world, entity); - if (toxic != null) { - return toxic.finishUsing(stack, world, entity); + if (toxic.isPresent()) { + return toxic.get().finishUsing(stack, world, entity); } return result; } @Override public TypedActionResult use(World world, PlayerEntity player, Hand hand) { - if (toxic == null) { + if (!toxic.isPresent()) { return super.use(world, player, hand); } - return toxic.use(world, player, hand, () -> super.use(world, player, hand)); + return toxic.get().use(world, player, hand, () -> super.use(world, player, hand)); } }