Added creative tabs

This commit is contained in:
Sollace 2020-09-23 22:38:28 +02:00
parent 20cdfa4822
commit 607760e4a6
7 changed files with 54 additions and 73 deletions

View file

@ -1,12 +1,18 @@
package com.minelittlepony.unicopia.item; 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.USounds;
import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
import com.minelittlepony.unicopia.item.toxin.Toxics; import com.minelittlepony.unicopia.item.toxin.Toxics;
import com.minelittlepony.unicopia.item.toxin.UFoodComponents; import com.minelittlepony.unicopia.item.toxin.UFoodComponents;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.Item.Settings; import net.minecraft.item.Item.Settings;
import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemGroup;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.minecraft.item.BlockItem; import net.minecraft.item.BlockItem;
import net.minecraft.item.FoodComponents; import net.minecraft.item.FoodComponents;
import net.minecraft.item.MusicDiscItem; import net.minecraft.item.MusicDiscItem;
@ -17,6 +23,8 @@ import net.minecraft.util.registry.Registry;
public interface UItems { public interface UItems {
List<Item> ITEMS = new ArrayList<>();
AppleItem GREEN_APPLE = register("green_apple", new AppleItem(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE))); 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 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))); 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); Item MUSIC_DISC_FUNK = register("music_disc_funk", USounds.RECORD_FUNK);
static <T extends Item> T register(String name, T item) { static <T extends Item> T register(String name, T item) {
ITEMS.add(item);
if (item instanceof BlockItem) { if (item instanceof BlockItem) {
((BlockItem)item).appendBlocks(Item.BLOCK_ITEMS, item); ((BlockItem)item).appendBlocks(Item.BLOCK_ITEMS, item);
} }
@ -48,5 +57,17 @@ public interface UItems {
static void bootstrap() { static void bootstrap() {
Toxics.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();
} }
} }

View file

@ -8,7 +8,6 @@ import com.minelittlepony.unicopia.entity.player.Pony;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
@ -19,18 +18,15 @@ import net.minecraft.world.World;
public class Toxic { public class Toxic {
private final Item item;
private final UseAction action; private final UseAction action;
private final Function<ItemStack, Toxicity> toxicity; private final Function<ItemStack, Toxicity> toxicity;
private final Toxin toxin; private final Toxin toxin;
Toxic(Item item, UseAction action, Toxin toxin, Toxicity toxicity) { Toxic(UseAction action, Toxin toxin, Toxicity toxicity) {
this(item, action, toxin, stack -> toxicity); this(action, toxin, stack -> toxicity);
} }
Toxic(Item item, UseAction action, Toxin toxin, Function<ItemStack, Toxicity> toxicity) { Toxic(UseAction action, Toxin toxin, Function<ItemStack, Toxicity> toxicity) {
this.item = item;
this.action = action; this.action = action;
this.toxin = toxin; this.toxin = toxin;
this.toxicity = toxicity; this.toxicity = toxicity;
@ -52,7 +48,11 @@ public class Toxic {
toxin.afflict((PlayerEntity)entity, t, stack); 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<ItemStack> use(World world, PlayerEntity player, Hand hand, Supplier<TypedActionResult<ItemStack>> sup) { public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand, Supplier<TypedActionResult<ItemStack>> sup) {

View file

@ -1,8 +1,15 @@
package com.minelittlepony.unicopia.item.toxin; package com.minelittlepony.unicopia.item.toxin;
import java.util.Optional;
import net.minecraft.item.FoodComponent; import net.minecraft.item.FoodComponent;
public interface ToxicHolder { public interface ToxicHolder {
void setFood(FoodComponent food); void setFood(FoodComponent food);
void setToxic(Toxic toxic); void setToxic(Toxic toxic);
default Optional<Toxic> getToxic() {
return Optional.empty();
}
} }

View file

@ -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<ItemStack, Toxicity> 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<Text> 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<ItemStack> use(World world, PlayerEntity player, Hand hand) {
return toxic.use(world, player, hand, () -> super.use(world, player, hand));
}
}

View file

@ -31,7 +31,7 @@ public interface Toxics {
static void bootstrap() {} static void bootstrap() {}
static Toxic register(Item target, FoodComponent food, UseAction action, Toxicity toxicity, Toxin toxin) { 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; ToxicHolder holder = (ToxicHolder)target;
holder.setFood(food); holder.setFood(food);
holder.setToxic(toxic); holder.setToxic(toxic);

View file

@ -1,6 +1,7 @@
package com.minelittlepony.unicopia.mixin; package com.minelittlepony.unicopia.mixin;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -28,43 +29,47 @@ import net.minecraft.world.World;
abstract class MixinBlockItem extends Item implements ToxicHolder { abstract class MixinBlockItem extends Item implements ToxicHolder {
public MixinBlockItem() {super(null); } public MixinBlockItem() {super(null); }
@Nullable private Optional<Toxic> toxic = Optional.empty();
private Toxic toxic;
@Override @Override
public void setToxic(Toxic toxic) { public void setToxic(Toxic toxic) {
this.toxic = toxic; this.toxic = Optional.of(toxic);
}
@Override
public Optional<Toxic> getToxic() {
return toxic;
} }
@Override @Override
public UseAction getUseAction(ItemStack stack) { public UseAction getUseAction(ItemStack stack) {
if (toxic != null) { if (toxic.isPresent()) {
return toxic.getUseAction(stack); return toxic.get().getUseAction(stack);
} }
return super.getUseAction(stack); return super.getUseAction(stack);
} }
@Inject(method = "appendTooltip", at = @At("RETURN")) @Inject(method = "appendTooltip", at = @At("RETURN"))
private void onAppendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context, CallbackInfo into) { private void onAppendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context, CallbackInfo into) {
if (toxic != null) { if (toxic.isPresent()) {
tooltip.add(toxic.getTooltip(stack)); tooltip.add(toxic.get().getTooltip(stack));
} }
} }
@Override @Override
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) { public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) {
ItemStack result = super.finishUsing(stack, world, entity); ItemStack result = super.finishUsing(stack, world, entity);
if (toxic != null) { if (toxic.isPresent()) {
return toxic.finishUsing(stack, world, entity); return toxic.get().finishUsing(stack, world, entity);
} }
return result; return result;
} }
@Override @Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (toxic == null) { if (!toxic.isPresent()) {
return super.use(world, player, hand); 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));
} }
} }