Skip to content

Commit

Permalink
Rewrite items in Java
Browse files Browse the repository at this point in the history
Fixes and changes:
  AdornBookItem.use:
    fix returning ActionResult.SUCCESS on the server
  CarpetedTopPlacementContext:
    use copying super ctor to simplify ctor
  ChairBlockItem.useOnBlock,
  TableBlockItem.useOnBlock:
    propagate action result from place()
  • Loading branch information
Juuxel committed Aug 1, 2024
1 parent 71ea3ac commit a196c91
Show file tree
Hide file tree
Showing 32 changed files with 560 additions and 499 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public int[] getAvailableSlots(Direction side) {

@Override
public boolean isValid(int slot, ItemStack stack) {
if (slot == INPUT_SLOT && !(stack.isOf(AdornItems.INSTANCE.getMUG()) && getStack(slot).isEmpty())) return false;
if (slot == INPUT_SLOT && !(stack.isOf(AdornItems.MUG.get()) && getStack(slot).isEmpty())) return false;
if (slot == FLUID_CONTAINER_SLOT && !getStack(slot).isEmpty()) return false;
return true;
}
Expand All @@ -116,7 +116,7 @@ public boolean canExtract(int slot, ItemStack stack, Direction dir) {
public int calculateComparatorOutput() {
// If brewing has finished
var mugStack = getStack(INPUT_SLOT);
if (!mugStack.isEmpty() && !mugStack.isOf(AdornItems.INSTANCE.getMUG())) {
if (!mugStack.isEmpty() && !mugStack.isOf(AdornItems.MUG.get())) {
return 15;
}

Expand Down Expand Up @@ -163,7 +163,7 @@ public static void tick(World world, BlockPos pos, BlockState state, BrewerBlock

var recipe = world.getRecipeManager().getFirstMatch(AdornRecipes.BREWING_TYPE.get(), brewer, world).map(RecipeEntry::value).orElse(null);

if (recipe != null && brewer.getStack(INPUT_SLOT).isOf(AdornItems.INSTANCE.getMUG())) {
if (recipe != null && brewer.getStack(INPUT_SLOT).isOf(AdornItems.MUG.get())) {
if (brewer.progress++ >= MAX_PROGRESS) {
decrementIngredient(brewer, LEFT_INGREDIENT_SLOT);
decrementIngredient(brewer, RIGHT_INGREDIENT_SLOT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public record BrewingEmiRecipe(
public BrewingEmiRecipe(Identifier id, ItemBrewingRecipe recipe) {
this(
id,
EmiStack.of(AdornItems.INSTANCE.getMUG()),
EmiStack.of(AdornItems.MUG.get()),
EmiUtil.withRemainders(EmiIngredient.of(recipe.firstIngredient())),
EmiUtil.withRemainders(EmiIngredient.of(recipe.secondIngredient())),
EmiStack.EMPTY,
Expand All @@ -43,7 +43,7 @@ public BrewingEmiRecipe(Identifier id, ItemBrewingRecipe recipe) {
public BrewingEmiRecipe(Identifier id, FluidBrewingRecipe recipe) {
this(
id,
EmiStack.of(AdornItems.INSTANCE.getMUG()),
EmiStack.of(AdornItems.MUG.get()),
EmiUtil.withRemainders(EmiIngredient.of(recipe.firstIngredient())),
EmiUtil.withRemainders(EmiIngredient.of(recipe.secondIngredient())),
EmiUtil.emiIngredientOf(recipe.fluid()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void setRecipe(IRecipeLayoutBuilder layoutBuilder, BrewingRecipe recipe,
var secondSlot = layoutBuilder.addSlot(RecipeIngredientRole.INPUT, leftX + 61, topY + 1);
var resultSlot = layoutBuilder.addSlot(RecipeIngredientRole.OUTPUT, leftX + 31, topY + 40);
layoutBuilder.addSlot(RecipeIngredientRole.INPUT, leftX + 4, topY + 39)
.addIngredients(Ingredient.ofItems(AdornItems.INSTANCE.getMUG()));
.addIngredients(Ingredient.ofItems(AdornItems.MUG.get()));
var capacity = FluidBridge.get().getFluidUnit().getBucketVolume() * BrewerBlockEntity.FLUID_CAPACITY_IN_BUCKETS;
var tank = layoutBuilder.addSlot(RecipeIngredientRole.INPUT, leftX + 88, topY + 1)
.setFluidRenderer(capacity, false, 16, BrewerScreen.FLUID_AREA_HEIGHT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public record BrewerDisplay(
) implements Display {
public BrewerDisplay(ItemBrewingRecipe recipe) {
this(
EntryIngredients.of(AdornItems.INSTANCE.getMUG()),
EntryIngredients.of(AdornItems.MUG.get()),
EntryIngredients.ofIngredient(recipe.firstIngredient()),
EntryIngredients.ofIngredient(recipe.secondIngredient()),
EntryIngredient.empty(),
Expand All @@ -38,7 +38,7 @@ public BrewerDisplay(ItemBrewingRecipe recipe) {

public BrewerDisplay(FluidBrewingRecipe recipe) {
this(
EntryIngredients.of(AdornItems.INSTANCE.getMUG()),
EntryIngredients.of(AdornItems.MUG.get()),
EntryIngredients.ofIngredient(recipe.firstIngredient()),
EntryIngredients.ofIngredient(recipe.secondIngredient()),
entryIngredientOf(recipe.fluid()),
Expand Down
45 changes: 45 additions & 0 deletions common/src/main/java/juuxel/adorn/item/AdornBookItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package juuxel.adorn.item;

import juuxel.adorn.platform.PlatformBridges;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public final class AdornBookItem extends Item {
private final Identifier bookId;

public AdornBookItem(Identifier bookId, Settings settings) {
super(settings);
this.bookId = bookId;
}

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
if (!world.isClient) {
PlatformBridges.Companion.getNetwork().sendOpenBookPacket(user, bookId);
}
user.incrementStat(Stats.USED.getOrCreateStat(this));

return TypedActionResult.success(user.getStackInHand(hand), world.isClient);
}

@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);
var bookManager = PlatformBridges.Companion.getResources().getBookManager();
if (bookManager.contains(bookId)) {
tooltip.add(Text.translatable("book.byAuthor", bookManager.get(bookId).author()).formatted(Formatting.GRAY));
}
}
}
57 changes: 57 additions & 0 deletions common/src/main/java/juuxel/adorn/item/AdornItems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package juuxel.adorn.item;

import juuxel.adorn.AdornCommon;
import juuxel.adorn.block.AdornBlocks;
import juuxel.adorn.lib.registry.Registered;
import juuxel.adorn.lib.registry.Registrar;
import juuxel.adorn.lib.registry.RegistrarFactory;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Rarity;
import net.minecraft.util.math.Direction;

public final class AdornItems {
public static final Registrar<Item> ITEMS = RegistrarFactory.get().create(RegistryKeys.ITEM);
private static final FoodComponent DRINK_FOOD_COMPONENT = drinkFoodComponentBuilder().build();

public static final Registered<Item> STONE_ROD = ITEMS.register("stone_rod", () -> new ItemWithDescription(new Item.Settings()));
public static final Registered<Item> MUG = ITEMS.register("mug", () -> new ItemWithDescription(new Item.Settings().maxCount(16)));
public static final Registered<Item> HOT_CHOCOLATE = ITEMS.register("hot_chocolate",
() -> new DrinkInMugItem(new Item.Settings().food(DRINK_FOOD_COMPONENT).maxCount(1)));
public static final Registered<Item> SWEET_BERRY_JUICE = ITEMS.register("sweet_berry_juice",
() -> new DrinkInMugItem(new Item.Settings().food(DRINK_FOOD_COMPONENT).maxCount(1)));
public static final Registered<Item> GLOW_BERRY_TEA = ITEMS.register("glow_berry_tea",
() -> new DrinkInMugItem(
new Item.Settings()
.food(drinkFoodComponentBuilder().statusEffect(new StatusEffectInstance(StatusEffects.GLOWING, 400), 1.0f).build())
.maxCount(1)
));
public static final Registered<Item> NETHER_WART_COFFEE = ITEMS.register("nether_wart_coffee",
() -> new DrinkInMugItem(new Item.Settings().food(DRINK_FOOD_COMPONENT).maxCount(1)));

public static final Registered<Item> STONE_TORCH = ITEMS.register("stone_torch",
() -> new VerticallyAttachableBlockItemWithDescription(
AdornBlocks.STONE_TORCH_GROUND.get(),
AdornBlocks.STONE_TORCH_WALL.get(),
new Item.Settings(),
Direction.DOWN
));

public static final Registered<Item> GUIDE_BOOK = ITEMS.register("guide_book",
() -> new AdornBookItem(AdornCommon.id("guide"), new Item.Settings().rarity(Rarity.UNCOMMON)));
public static final Registered<Item> TRADERS_MANUAL = ITEMS.register("traders_manual",
() -> new AdornBookItem(AdornCommon.id("traders_manual"), new Item.Settings()));

public static final Registered<Item> COPPER_NUGGET = ITEMS.register("copper_nugget", () -> new ItemWithDescription(new Item.Settings()));
public static final Registered<Item> WATERING_CAN = ITEMS.register("watering_can", () -> new WateringCanItem(new Item.Settings()));

private static FoodComponent.Builder drinkFoodComponentBuilder() {
return new FoodComponent.Builder().hunger(4).saturationModifier(0.3F).alwaysEdible();
}

public static void init() {
}
}
27 changes: 27 additions & 0 deletions common/src/main/java/juuxel/adorn/item/AdornTallBlockItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package juuxel.adorn.item;

import juuxel.adorn.block.BlockWithDescription;
import net.minecraft.block.Block;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.TallBlockItem;
import net.minecraft.text.Text;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class AdornTallBlockItem extends TallBlockItem {
public AdornTallBlockItem(Block block, Settings settings) {
super(block, settings);
}

@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);

if (getBlock() instanceof BlockWithDescription withDescription) {
tooltip.add(ItemWithDescription.createDescriptionText(withDescription.getDescriptionKey()));
}
}
}
27 changes: 27 additions & 0 deletions common/src/main/java/juuxel/adorn/item/BaseBlockItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package juuxel.adorn.item;

import juuxel.adorn.block.BlockWithDescription;
import net.minecraft.block.Block;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class BaseBlockItem extends BlockItem {
public BaseBlockItem(Block block, Settings settings) {
super(block, settings);
}

@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);

if (getBlock() instanceof BlockWithDescription withDescription) {
tooltip.add(ItemWithDescription.createDescriptionText(withDescription.getDescriptionKey()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package juuxel.adorn.item;

import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemUsageContext;

public final class CarpetedTopPlacementContext extends ItemPlacementContext {
public CarpetedTopPlacementContext(ItemUsageContext context) {
super(context);
// We know that the block is a carpet block
canReplaceExisting = true;
}
}
24 changes: 24 additions & 0 deletions common/src/main/java/juuxel/adorn/item/ChairBlockItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package juuxel.adorn.item;

import net.minecraft.block.Block;
import net.minecraft.block.CarpetBlock;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.Direction;

public final class ChairBlockItem extends AdornTallBlockItem {
public ChairBlockItem(Block block) {
super(block, new Settings());
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
var world = context.getWorld();
var pos = context.getBlockPos();
if (context.getSide() == Direction.UP && world.getBlockState(pos).getBlock() instanceof CarpetBlock) {
return place(new CarpetedTopPlacementContext(context));
}

return super.useOnBlock(context);
}
}
30 changes: 30 additions & 0 deletions common/src/main/java/juuxel/adorn/item/DrinkInMugItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package juuxel.adorn.item;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.UseAction;
import net.minecraft.world.World;

public class DrinkInMugItem extends ItemWithDescription {
public DrinkInMugItem(Settings settings) {
super(settings);
}

@Override
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {
var result = super.finishUsing(stack, world, user);
return user instanceof PlayerEntity player && player.getAbilities().creativeMode ? result : new ItemStack(AdornItems.MUG.get());
}

@Override
public UseAction getUseAction(ItemStack stack) {
return UseAction.DRINK;
}

@Override
public SoundEvent getEatSound() {
return getDrinkSound();
}
}
28 changes: 28 additions & 0 deletions common/src/main/java/juuxel/adorn/item/ItemWithDescription.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package juuxel.adorn.item;

import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class ItemWithDescription extends Item {
public ItemWithDescription(Settings settings) {
super(settings);
}

@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);
tooltip.add(createDescriptionText(getTranslationKey() + ".description"));
}

public static Text createDescriptionText(String translationKey) {
return Text.translatable(translationKey)
.styled(style -> style.withItalic(true).withColor(Formatting.DARK_GRAY));
}
}
24 changes: 24 additions & 0 deletions common/src/main/java/juuxel/adorn/item/TableBlockItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package juuxel.adorn.item;

import net.minecraft.block.Block;
import net.minecraft.block.CarpetBlock;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.Direction;

public final class TableBlockItem extends BaseBlockItem {
public TableBlockItem(Block block) {
super(block, new Settings());
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
var world = context.getWorld();
var pos = context.getBlockPos();
if (context.getSide() == Direction.UP && world.getBlockState(pos).getBlock() instanceof CarpetBlock) {
return place(new CarpetedTopPlacementContext(context));
}

return super.useOnBlock(context);
}
}
38 changes: 38 additions & 0 deletions common/src/main/java/juuxel/adorn/item/TradingStationItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package juuxel.adorn.item;

import juuxel.adorn.block.entity.TradingStationBlockEntity;
import juuxel.adorn.trading.Trade;
import juuxel.adorn.util.NbtExtensionsKt;
import net.minecraft.block.Block;
import net.minecraft.client.item.TooltipData;
import net.minecraft.item.ItemStack;

import java.util.Optional;

public final class TradingStationItem extends BaseBlockItem {
public TradingStationItem(Block block, Settings settings) {
super(block, settings);
}

@Override
public boolean canBeNested() {
// Don't allow putting trading stations inside shulker boxes or other trading stations.
return false;
}

@Override
public Optional<TooltipData> getTooltipData(ItemStack stack) {
var nbt = stack.getSubNbt(BLOCK_ENTITY_TAG_KEY);
if (nbt != null) {
var tradeNbt = NbtExtensionsKt.getCompoundOrNull(nbt, TradingStationBlockEntity.NBT_TRADE);
if (tradeNbt != null) {
var trade = Trade.fromNbt(tradeNbt);
if (!trade.isFullyEmpty()) {
return Optional.of(trade);
}
}
}

return Optional.empty();
}
}
Loading

0 comments on commit a196c91

Please sign in to comment.