-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
32 changed files
with
560 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
common/src/main/java/juuxel/adorn/item/AdornTallBlockItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
common/src/main/java/juuxel/adorn/item/CarpetedTopPlacementContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
common/src/main/java/juuxel/adorn/item/ChairBlockItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
common/src/main/java/juuxel/adorn/item/DrinkInMugItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
common/src/main/java/juuxel/adorn/item/ItemWithDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
common/src/main/java/juuxel/adorn/item/TableBlockItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
common/src/main/java/juuxel/adorn/item/TradingStationItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.