Skip to content

Commit a196c91

Browse files
committed
Rewrite items in Java
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()
1 parent 71ea3ac commit a196c91

32 files changed

+560
-499
lines changed

common/src/main/java/juuxel/adorn/block/entity/BrewerBlockEntity.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public int[] getAvailableSlots(Direction side) {
9898

9999
@Override
100100
public boolean isValid(int slot, ItemStack stack) {
101-
if (slot == INPUT_SLOT && !(stack.isOf(AdornItems.INSTANCE.getMUG()) && getStack(slot).isEmpty())) return false;
101+
if (slot == INPUT_SLOT && !(stack.isOf(AdornItems.MUG.get()) && getStack(slot).isEmpty())) return false;
102102
if (slot == FLUID_CONTAINER_SLOT && !getStack(slot).isEmpty()) return false;
103103
return true;
104104
}
@@ -116,7 +116,7 @@ public boolean canExtract(int slot, ItemStack stack, Direction dir) {
116116
public int calculateComparatorOutput() {
117117
// If brewing has finished
118118
var mugStack = getStack(INPUT_SLOT);
119-
if (!mugStack.isEmpty() && !mugStack.isOf(AdornItems.INSTANCE.getMUG())) {
119+
if (!mugStack.isEmpty() && !mugStack.isOf(AdornItems.MUG.get())) {
120120
return 15;
121121
}
122122

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

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

166-
if (recipe != null && brewer.getStack(INPUT_SLOT).isOf(AdornItems.INSTANCE.getMUG())) {
166+
if (recipe != null && brewer.getStack(INPUT_SLOT).isOf(AdornItems.MUG.get())) {
167167
if (brewer.progress++ >= MAX_PROGRESS) {
168168
decrementIngredient(brewer, LEFT_INGREDIENT_SLOT);
169169
decrementIngredient(brewer, RIGHT_INGREDIENT_SLOT);

common/src/main/java/juuxel/adorn/compat/emi/BrewingEmiRecipe.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public record BrewingEmiRecipe(
3232
public BrewingEmiRecipe(Identifier id, ItemBrewingRecipe recipe) {
3333
this(
3434
id,
35-
EmiStack.of(AdornItems.INSTANCE.getMUG()),
35+
EmiStack.of(AdornItems.MUG.get()),
3636
EmiUtil.withRemainders(EmiIngredient.of(recipe.firstIngredient())),
3737
EmiUtil.withRemainders(EmiIngredient.of(recipe.secondIngredient())),
3838
EmiStack.EMPTY,
@@ -43,7 +43,7 @@ public BrewingEmiRecipe(Identifier id, ItemBrewingRecipe recipe) {
4343
public BrewingEmiRecipe(Identifier id, FluidBrewingRecipe recipe) {
4444
this(
4545
id,
46-
EmiStack.of(AdornItems.INSTANCE.getMUG()),
46+
EmiStack.of(AdornItems.MUG.get()),
4747
EmiUtil.withRemainders(EmiIngredient.of(recipe.firstIngredient())),
4848
EmiUtil.withRemainders(EmiIngredient.of(recipe.secondIngredient())),
4949
EmiUtil.emiIngredientOf(recipe.fluid()),

common/src/main/java/juuxel/adorn/compat/jei/BrewerCategory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void setRecipe(IRecipeLayoutBuilder layoutBuilder, BrewingRecipe recipe,
6363
var secondSlot = layoutBuilder.addSlot(RecipeIngredientRole.INPUT, leftX + 61, topY + 1);
6464
var resultSlot = layoutBuilder.addSlot(RecipeIngredientRole.OUTPUT, leftX + 31, topY + 40);
6565
layoutBuilder.addSlot(RecipeIngredientRole.INPUT, leftX + 4, topY + 39)
66-
.addIngredients(Ingredient.ofItems(AdornItems.INSTANCE.getMUG()));
66+
.addIngredients(Ingredient.ofItems(AdornItems.MUG.get()));
6767
var capacity = FluidBridge.get().getFluidUnit().getBucketVolume() * BrewerBlockEntity.FLUID_CAPACITY_IN_BUCKETS;
6868
var tank = layoutBuilder.addSlot(RecipeIngredientRole.INPUT, leftX + 88, topY + 1)
6969
.setFluidRenderer(capacity, false, 16, BrewerScreen.FLUID_AREA_HEIGHT)

common/src/main/java/juuxel/adorn/compat/rei/BrewerDisplay.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public record BrewerDisplay(
2828
) implements Display {
2929
public BrewerDisplay(ItemBrewingRecipe recipe) {
3030
this(
31-
EntryIngredients.of(AdornItems.INSTANCE.getMUG()),
31+
EntryIngredients.of(AdornItems.MUG.get()),
3232
EntryIngredients.ofIngredient(recipe.firstIngredient()),
3333
EntryIngredients.ofIngredient(recipe.secondIngredient()),
3434
EntryIngredient.empty(),
@@ -38,7 +38,7 @@ public BrewerDisplay(ItemBrewingRecipe recipe) {
3838

3939
public BrewerDisplay(FluidBrewingRecipe recipe) {
4040
this(
41-
EntryIngredients.of(AdornItems.INSTANCE.getMUG()),
41+
EntryIngredients.of(AdornItems.MUG.get()),
4242
EntryIngredients.ofIngredient(recipe.firstIngredient()),
4343
EntryIngredients.ofIngredient(recipe.secondIngredient()),
4444
entryIngredientOf(recipe.fluid()),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package juuxel.adorn.item;
2+
3+
import juuxel.adorn.platform.PlatformBridges;
4+
import net.minecraft.client.item.TooltipContext;
5+
import net.minecraft.entity.player.PlayerEntity;
6+
import net.minecraft.item.Item;
7+
import net.minecraft.item.ItemStack;
8+
import net.minecraft.stat.Stats;
9+
import net.minecraft.text.Text;
10+
import net.minecraft.util.Formatting;
11+
import net.minecraft.util.Hand;
12+
import net.minecraft.util.Identifier;
13+
import net.minecraft.util.TypedActionResult;
14+
import net.minecraft.world.World;
15+
import org.jetbrains.annotations.Nullable;
16+
17+
import java.util.List;
18+
19+
public final class AdornBookItem extends Item {
20+
private final Identifier bookId;
21+
22+
public AdornBookItem(Identifier bookId, Settings settings) {
23+
super(settings);
24+
this.bookId = bookId;
25+
}
26+
27+
@Override
28+
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
29+
if (!world.isClient) {
30+
PlatformBridges.Companion.getNetwork().sendOpenBookPacket(user, bookId);
31+
}
32+
user.incrementStat(Stats.USED.getOrCreateStat(this));
33+
34+
return TypedActionResult.success(user.getStackInHand(hand), world.isClient);
35+
}
36+
37+
@Override
38+
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
39+
super.appendTooltip(stack, world, tooltip, context);
40+
var bookManager = PlatformBridges.Companion.getResources().getBookManager();
41+
if (bookManager.contains(bookId)) {
42+
tooltip.add(Text.translatable("book.byAuthor", bookManager.get(bookId).author()).formatted(Formatting.GRAY));
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package juuxel.adorn.item;
2+
3+
import juuxel.adorn.AdornCommon;
4+
import juuxel.adorn.block.AdornBlocks;
5+
import juuxel.adorn.lib.registry.Registered;
6+
import juuxel.adorn.lib.registry.Registrar;
7+
import juuxel.adorn.lib.registry.RegistrarFactory;
8+
import net.minecraft.entity.effect.StatusEffectInstance;
9+
import net.minecraft.entity.effect.StatusEffects;
10+
import net.minecraft.item.FoodComponent;
11+
import net.minecraft.item.Item;
12+
import net.minecraft.registry.RegistryKeys;
13+
import net.minecraft.util.Rarity;
14+
import net.minecraft.util.math.Direction;
15+
16+
public final class AdornItems {
17+
public static final Registrar<Item> ITEMS = RegistrarFactory.get().create(RegistryKeys.ITEM);
18+
private static final FoodComponent DRINK_FOOD_COMPONENT = drinkFoodComponentBuilder().build();
19+
20+
public static final Registered<Item> STONE_ROD = ITEMS.register("stone_rod", () -> new ItemWithDescription(new Item.Settings()));
21+
public static final Registered<Item> MUG = ITEMS.register("mug", () -> new ItemWithDescription(new Item.Settings().maxCount(16)));
22+
public static final Registered<Item> HOT_CHOCOLATE = ITEMS.register("hot_chocolate",
23+
() -> new DrinkInMugItem(new Item.Settings().food(DRINK_FOOD_COMPONENT).maxCount(1)));
24+
public static final Registered<Item> SWEET_BERRY_JUICE = ITEMS.register("sweet_berry_juice",
25+
() -> new DrinkInMugItem(new Item.Settings().food(DRINK_FOOD_COMPONENT).maxCount(1)));
26+
public static final Registered<Item> GLOW_BERRY_TEA = ITEMS.register("glow_berry_tea",
27+
() -> new DrinkInMugItem(
28+
new Item.Settings()
29+
.food(drinkFoodComponentBuilder().statusEffect(new StatusEffectInstance(StatusEffects.GLOWING, 400), 1.0f).build())
30+
.maxCount(1)
31+
));
32+
public static final Registered<Item> NETHER_WART_COFFEE = ITEMS.register("nether_wart_coffee",
33+
() -> new DrinkInMugItem(new Item.Settings().food(DRINK_FOOD_COMPONENT).maxCount(1)));
34+
35+
public static final Registered<Item> STONE_TORCH = ITEMS.register("stone_torch",
36+
() -> new VerticallyAttachableBlockItemWithDescription(
37+
AdornBlocks.STONE_TORCH_GROUND.get(),
38+
AdornBlocks.STONE_TORCH_WALL.get(),
39+
new Item.Settings(),
40+
Direction.DOWN
41+
));
42+
43+
public static final Registered<Item> GUIDE_BOOK = ITEMS.register("guide_book",
44+
() -> new AdornBookItem(AdornCommon.id("guide"), new Item.Settings().rarity(Rarity.UNCOMMON)));
45+
public static final Registered<Item> TRADERS_MANUAL = ITEMS.register("traders_manual",
46+
() -> new AdornBookItem(AdornCommon.id("traders_manual"), new Item.Settings()));
47+
48+
public static final Registered<Item> COPPER_NUGGET = ITEMS.register("copper_nugget", () -> new ItemWithDescription(new Item.Settings()));
49+
public static final Registered<Item> WATERING_CAN = ITEMS.register("watering_can", () -> new WateringCanItem(new Item.Settings()));
50+
51+
private static FoodComponent.Builder drinkFoodComponentBuilder() {
52+
return new FoodComponent.Builder().hunger(4).saturationModifier(0.3F).alwaysEdible();
53+
}
54+
55+
public static void init() {
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package juuxel.adorn.item;
2+
3+
import juuxel.adorn.block.BlockWithDescription;
4+
import net.minecraft.block.Block;
5+
import net.minecraft.client.item.TooltipContext;
6+
import net.minecraft.item.ItemStack;
7+
import net.minecraft.item.TallBlockItem;
8+
import net.minecraft.text.Text;
9+
import net.minecraft.world.World;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.List;
13+
14+
public class AdornTallBlockItem extends TallBlockItem {
15+
public AdornTallBlockItem(Block block, Settings settings) {
16+
super(block, settings);
17+
}
18+
19+
@Override
20+
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
21+
super.appendTooltip(stack, world, tooltip, context);
22+
23+
if (getBlock() instanceof BlockWithDescription withDescription) {
24+
tooltip.add(ItemWithDescription.createDescriptionText(withDescription.getDescriptionKey()));
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package juuxel.adorn.item;
2+
3+
import juuxel.adorn.block.BlockWithDescription;
4+
import net.minecraft.block.Block;
5+
import net.minecraft.client.item.TooltipContext;
6+
import net.minecraft.item.BlockItem;
7+
import net.minecraft.item.ItemStack;
8+
import net.minecraft.text.Text;
9+
import net.minecraft.world.World;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.List;
13+
14+
public class BaseBlockItem extends BlockItem {
15+
public BaseBlockItem(Block block, Settings settings) {
16+
super(block, settings);
17+
}
18+
19+
@Override
20+
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
21+
super.appendTooltip(stack, world, tooltip, context);
22+
23+
if (getBlock() instanceof BlockWithDescription withDescription) {
24+
tooltip.add(ItemWithDescription.createDescriptionText(withDescription.getDescriptionKey()));
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package juuxel.adorn.item;
2+
3+
import net.minecraft.item.ItemPlacementContext;
4+
import net.minecraft.item.ItemUsageContext;
5+
6+
public final class CarpetedTopPlacementContext extends ItemPlacementContext {
7+
public CarpetedTopPlacementContext(ItemUsageContext context) {
8+
super(context);
9+
// We know that the block is a carpet block
10+
canReplaceExisting = true;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package juuxel.adorn.item;
2+
3+
import net.minecraft.block.Block;
4+
import net.minecraft.block.CarpetBlock;
5+
import net.minecraft.item.ItemUsageContext;
6+
import net.minecraft.util.ActionResult;
7+
import net.minecraft.util.math.Direction;
8+
9+
public final class ChairBlockItem extends AdornTallBlockItem {
10+
public ChairBlockItem(Block block) {
11+
super(block, new Settings());
12+
}
13+
14+
@Override
15+
public ActionResult useOnBlock(ItemUsageContext context) {
16+
var world = context.getWorld();
17+
var pos = context.getBlockPos();
18+
if (context.getSide() == Direction.UP && world.getBlockState(pos).getBlock() instanceof CarpetBlock) {
19+
return place(new CarpetedTopPlacementContext(context));
20+
}
21+
22+
return super.useOnBlock(context);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package juuxel.adorn.item;
2+
3+
import net.minecraft.entity.LivingEntity;
4+
import net.minecraft.entity.player.PlayerEntity;
5+
import net.minecraft.item.ItemStack;
6+
import net.minecraft.sound.SoundEvent;
7+
import net.minecraft.util.UseAction;
8+
import net.minecraft.world.World;
9+
10+
public class DrinkInMugItem extends ItemWithDescription {
11+
public DrinkInMugItem(Settings settings) {
12+
super(settings);
13+
}
14+
15+
@Override
16+
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {
17+
var result = super.finishUsing(stack, world, user);
18+
return user instanceof PlayerEntity player && player.getAbilities().creativeMode ? result : new ItemStack(AdornItems.MUG.get());
19+
}
20+
21+
@Override
22+
public UseAction getUseAction(ItemStack stack) {
23+
return UseAction.DRINK;
24+
}
25+
26+
@Override
27+
public SoundEvent getEatSound() {
28+
return getDrinkSound();
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package juuxel.adorn.item;
2+
3+
import net.minecraft.client.item.TooltipContext;
4+
import net.minecraft.item.Item;
5+
import net.minecraft.item.ItemStack;
6+
import net.minecraft.text.Text;
7+
import net.minecraft.util.Formatting;
8+
import net.minecraft.world.World;
9+
import org.jetbrains.annotations.Nullable;
10+
11+
import java.util.List;
12+
13+
public class ItemWithDescription extends Item {
14+
public ItemWithDescription(Settings settings) {
15+
super(settings);
16+
}
17+
18+
@Override
19+
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
20+
super.appendTooltip(stack, world, tooltip, context);
21+
tooltip.add(createDescriptionText(getTranslationKey() + ".description"));
22+
}
23+
24+
public static Text createDescriptionText(String translationKey) {
25+
return Text.translatable(translationKey)
26+
.styled(style -> style.withItalic(true).withColor(Formatting.DARK_GRAY));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package juuxel.adorn.item;
2+
3+
import net.minecraft.block.Block;
4+
import net.minecraft.block.CarpetBlock;
5+
import net.minecraft.item.ItemUsageContext;
6+
import net.minecraft.util.ActionResult;
7+
import net.minecraft.util.math.Direction;
8+
9+
public final class TableBlockItem extends BaseBlockItem {
10+
public TableBlockItem(Block block) {
11+
super(block, new Settings());
12+
}
13+
14+
@Override
15+
public ActionResult useOnBlock(ItemUsageContext context) {
16+
var world = context.getWorld();
17+
var pos = context.getBlockPos();
18+
if (context.getSide() == Direction.UP && world.getBlockState(pos).getBlock() instanceof CarpetBlock) {
19+
return place(new CarpetedTopPlacementContext(context));
20+
}
21+
22+
return super.useOnBlock(context);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package juuxel.adorn.item;
2+
3+
import juuxel.adorn.block.entity.TradingStationBlockEntity;
4+
import juuxel.adorn.trading.Trade;
5+
import juuxel.adorn.util.NbtExtensionsKt;
6+
import net.minecraft.block.Block;
7+
import net.minecraft.client.item.TooltipData;
8+
import net.minecraft.item.ItemStack;
9+
10+
import java.util.Optional;
11+
12+
public final class TradingStationItem extends BaseBlockItem {
13+
public TradingStationItem(Block block, Settings settings) {
14+
super(block, settings);
15+
}
16+
17+
@Override
18+
public boolean canBeNested() {
19+
// Don't allow putting trading stations inside shulker boxes or other trading stations.
20+
return false;
21+
}
22+
23+
@Override
24+
public Optional<TooltipData> getTooltipData(ItemStack stack) {
25+
var nbt = stack.getSubNbt(BLOCK_ENTITY_TAG_KEY);
26+
if (nbt != null) {
27+
var tradeNbt = NbtExtensionsKt.getCompoundOrNull(nbt, TradingStationBlockEntity.NBT_TRADE);
28+
if (tradeNbt != null) {
29+
var trade = Trade.fromNbt(tradeNbt);
30+
if (!trade.isFullyEmpty()) {
31+
return Optional.of(trade);
32+
}
33+
}
34+
}
35+
36+
return Optional.empty();
37+
}
38+
}

0 commit comments

Comments
 (0)