diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a63b0e..ac060f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,22 @@ After publishing, the action commits the next development version and changelog ## Unreleased +- The pottery wheel fills the block it stands on and the lower half of the block above. The outline is that whole shape, so nothing else can be placed there. Breaking either part drops one wheel, and a wheel from an older world grows the upper half when the space above it is empty. +- Right-click the table with a blueprint to set it on the wheel. That uses one blueprint, even in Creative. Right-click the table with an empty hand to take it back. Setting another blueprint returns the one that was there. Breaking the wheel in Survival drops the blueprint along with any clay still on it. +- The blueprint's shape stands on all four edges of the table. +- Editing a stack of raw templates saves the shape onto one blueprint and leaves the rest of the stack raw. +- The template editor says left-click shapes the outside and right-click creates the inside. +- Pottery on a shelf sits on the plank, a little larger than before, and shows the liquid inside. +- Placed pottery keeps an even shade from one layer of the shape to the next. +- A wide mouth is a Bowl, a narrow mouth is a Jug, and any other shape is Pottery. A raw piece is named Raw Bowl, Raw Jug, or Raw Pottery. +- The creative tab contains the pottery wheel and the raw pottery template. +- On NeoForge 1.20.6, pottery items render in the inventory. +- A raw or fired piece in your hand or inventory has the shape that was thrown. Raw clay looks like clay, painted clay and fired pottery use a bright dye, and a closed pot keeps its lid. +- Raw pottery's tooltip says it needs firing. Undyed clay also says it can be dyed, and a vessel says how many millilitres it will hold once it is fired. +- A clay ball on the wheel is used up. The wheel stops when the shape is finished. Take the piece off then and you get raw pottery of that shape. Take it off earlier and you get the shape it had reached. +- Undyed raw pottery can go back on the wheel and be reshaped toward another blueprint. +- Craft raw pottery with a dye to paint it. Smelt raw pottery, painted or plain, and it fires into a usable vessel with the same shape and colour. + ## 0.0.1 - Picking up clay unlocks recipes for the pottery wheel and raw template. Mining either half of the wheel returns the wheel in Survival. diff --git a/fabric/src/main/java/net/modcrafters/minecraft/primalpottery/fabric/PrimalPotteryFabric.java b/fabric/src/main/java/net/modcrafters/minecraft/primalpottery/fabric/PrimalPotteryFabric.java index a6bfc7d..78e90bc 100644 --- a/fabric/src/main/java/net/modcrafters/minecraft/primalpottery/fabric/PrimalPotteryFabric.java +++ b/fabric/src/main/java/net/modcrafters/minecraft/primalpottery/fabric/PrimalPotteryFabric.java @@ -13,6 +13,7 @@ import net.modcrafters.minecraft.primalpottery.block.PotteryBlock; import net.modcrafters.minecraft.primalpottery.item.ModItems; import net.modcrafters.minecraft.primalpottery.item.ModTab; +import net.modcrafters.minecraft.primalpottery.recipe.ModRecipes; import net.modcrafters.minecraft.primalpottery.network.LidPayload; import net.modcrafters.minecraft.primalpottery.network.LidSync; import net.modcrafters.minecraft.primalpottery.network.PlacedLidPayload; @@ -26,6 +27,7 @@ public void onInitialize() { ModBlocks.register((id, block) -> Registry.register(BuiltInRegistries.BLOCK, id, block)); ModBlockEntities.register((id, type) -> Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, id, type)); ModItems.register((id, item) -> Registry.register(BuiltInRegistries.ITEM, id, item)); + ModRecipes.register((id, serializer) -> Registry.register(BuiltInRegistries.RECIPE_SERIALIZER, id, serializer)); Registry.register( BuiltInRegistries.CREATIVE_MODE_TAB, ModTab.ID, diff --git a/fabric/src/main/java/net/modcrafters/minecraft/primalpottery/fabric/client/PrimalPotteryFabricClient.java b/fabric/src/main/java/net/modcrafters/minecraft/primalpottery/fabric/client/PrimalPotteryFabricClient.java index f2fe7f9..7dd9bb1 100644 --- a/fabric/src/main/java/net/modcrafters/minecraft/primalpottery/fabric/client/PrimalPotteryFabricClient.java +++ b/fabric/src/main/java/net/modcrafters/minecraft/primalpottery/fabric/client/PrimalPotteryFabricClient.java @@ -7,12 +7,15 @@ import net.fabricmc.fabric.api.client.rendering.v1.level.LevelRenderEvents; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.rendertype.RenderTypes; +import net.minecraft.client.renderer.special.SpecialModelRenderers; import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.HitResult; import net.minecraft.world.phys.shapes.Shapes; +import net.modcrafters.minecraft.primalpottery.Ids; import net.modcrafters.minecraft.primalpottery.block.ModBlockEntities; import net.modcrafters.minecraft.primalpottery.client.PotteryPreview; +import net.modcrafters.minecraft.primalpottery.client.PotterySpecialRenderer; import net.modcrafters.minecraft.primalpottery.network.LidPayload; import net.modcrafters.minecraft.primalpottery.network.LidSync; import net.modcrafters.minecraft.primalpottery.network.PlacedLidPayload; @@ -31,6 +34,7 @@ public void onInitializeClient() { PlacedLidSync.sender = (pos, slot, closed) -> ClientPlayNetworking.send(new PlacedLidPayload(pos, slot, closed)); PictureInPictureRendererRegistry.register(context -> new PotteryPreview.Renderer()); PotteryPreview.SINK = (graphics, state) -> graphics.guiRenderState.addPicturesInPictureState(state); + SpecialModelRenderers.ID_MAPPER.put(Ids.of("pottery"), PotterySpecialRenderer.Unbaked.MAP_CODEC); BlockEntityRendererRegistry.register(ModBlockEntities.WHEEL, context -> new WheelRenderer()); BlockEntityRendererRegistry.register(ModBlockEntities.POTTERY, context -> new PotteryRenderer()); LevelRenderEvents.BEFORE_BLOCK_OUTLINE.register((context, outline) -> { diff --git a/fabric/src/main/resources/primalpottery.accesswidener b/fabric/src/main/resources/primalpottery.accesswidener index f5b8641..144aa2e 100644 --- a/fabric/src/main/resources/primalpottery.accesswidener +++ b/fabric/src/main/resources/primalpottery.accesswidener @@ -1,2 +1,3 @@ accessWidener v2 official accessible field net/minecraft/client/gui/GuiGraphicsExtractor guiRenderState Lnet/minecraft/client/renderer/state/gui/GuiRenderState; +accessible field net/minecraft/client/renderer/special/SpecialModelRenderers ID_MAPPER Lnet/minecraft/util/ExtraCodecs$LateBoundIdMapper; diff --git a/neoforge/build.gradle b/neoforge/build.gradle index b065317..98d7904 100644 --- a/neoforge/build.gradle +++ b/neoforge/build.gradle @@ -70,7 +70,10 @@ tasks.named('compileJava', JavaCompile) { neoForge { version = neoforge_version - def at = rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') + def at = file('src/main/resources/META-INF/accesstransformer.cfg') + if (!at.exists()) { + at = rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') + } if (at.exists()) { accessTransformers.from(at.absolutePath) } diff --git a/neoforge/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/PrimalPotteryNeoForge.java b/neoforge/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/PrimalPotteryNeoForge.java index 4962dc6..25477d5 100644 --- a/neoforge/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/PrimalPotteryNeoForge.java +++ b/neoforge/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/PrimalPotteryNeoForge.java @@ -8,6 +8,7 @@ import net.modcrafters.minecraft.primalpottery.block.ModBlocks; import net.modcrafters.minecraft.primalpottery.item.ModItems; import net.modcrafters.minecraft.primalpottery.item.ModTab; +import net.modcrafters.minecraft.primalpottery.recipe.ModRecipes; import net.modcrafters.minecraft.primalpottery.neoforge.client.PrimalPotteryNeoForgeClient; //? if >=26.1 import net.modcrafters.minecraft.primalpottery.neoforge.PotteryGameTests; @@ -68,6 +69,7 @@ private static void registerContent(RegisterEvent event) { event.register(Registries.BLOCK, helper -> ModBlocks.register(helper::register)); event.register(Registries.BLOCK_ENTITY_TYPE, helper -> ModBlockEntities.register(helper::register)); event.register(Registries.ITEM, helper -> ModItems.register(helper::register)); + event.register(Registries.RECIPE_SERIALIZER, helper -> ModRecipes.register(helper::register)); //? if >=26.1 { event.register(net.minecraft.core.registries.Registries.TEST_INSTANCE_TYPE, helper -> { helper.register(net.modcrafters.minecraft.primalpottery.Ids.of("wheel"), PotteryGameTests.codec()); diff --git a/neoforge/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/client/PrimalPotteryNeoForgeClient.java b/neoforge/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/client/PrimalPotteryNeoForgeClient.java index 10219f5..df2474b 100644 --- a/neoforge/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/client/PrimalPotteryNeoForgeClient.java +++ b/neoforge/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/client/PrimalPotteryNeoForgeClient.java @@ -24,6 +24,12 @@ //? if >=26.1 import net.neoforged.neoforge.client.event.RegisterPictureInPictureRenderersEvent; //? if >=26.1 +import net.neoforged.neoforge.client.event.RegisterSpecialModelRendererEvent; +//? if >=26.1 +import net.modcrafters.minecraft.primalpottery.client.PotterySpecialRenderer; +//? if >=26.1 +import net.modcrafters.minecraft.primalpottery.Ids; +//? if >=26.1 import net.minecraft.client.renderer.rendertype.RenderTypes; //? if >=26.1 import net.neoforged.neoforge.client.event.ExtractBlockOutlineRenderStateEvent; @@ -61,10 +67,20 @@ public static void register(IEventBus modBus) { *///?} }; modBus.addListener(PrimalPotteryNeoForgeClient::renderers); + //? if <26.1 { + /*modBus.addListener(PrimalPotteryNeoForgeClient::colors); + *///?} //? if >=26.1 { modBus.addListener(PrimalPotteryNeoForgeClient::pictures); + modBus.addListener(PrimalPotteryNeoForgeClient::specialModels); PotteryPreview.SINK = (graphics, state) -> graphics.submitPictureInPictureRenderState(state); //?} + //? if >=1.21 && <26.1 { + /*modBus.addListener(PrimalPotteryNeoForgeClient::items); + *///?} + //? if <1.21 { + /*modBus.addListener(PrimalPotteryNeoForgeClient::bindItems); + *///?} } public static void registerGame(IEventBus gameBus) { @@ -79,8 +95,63 @@ public static void registerGame(IEventBus gameBus) { private static void pictures(RegisterPictureInPictureRenderersEvent event) { event.register(PotteryPreview.State.class, PotteryPreview.Renderer::new); } + + private static void specialModels(RegisterSpecialModelRendererEvent event) { + event.register(Ids.of("pottery"), PotterySpecialRenderer.Unbaked.MAP_CODEC); + } //?} + //? if <26.1 { + /*private static net.neoforged.neoforge.client.extensions.common.IClientItemExtensions extensions() { + return new net.neoforged.neoforge.client.extensions.common.IClientItemExtensions() { + private net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer renderer; + + @Override + public net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer getCustomRenderer() { + if (this.renderer == null) { + var minecraft = net.minecraft.client.Minecraft.getInstance(); + this.renderer = new net.modcrafters.minecraft.primalpottery.client.PotteryItemRenderer( + minecraft.getBlockEntityRenderDispatcher(), minecraft.getEntityModels()); + } + return this.renderer; + } + }; + } + *///?} + + //? if >=1.21 && <26.1 { + /*private static void items(net.neoforged.neoforge.client.extensions.common.RegisterClientExtensionsEvent event) { + event.registerItem( + extensions(), + net.modcrafters.minecraft.primalpottery.item.ModItems.POTTERY, + net.modcrafters.minecraft.primalpottery.item.ModItems.RAW_POTTERY + ); + } + *///?} + + //? if <1.21 { + /*private static void bindItems(net.neoforged.fml.event.lifecycle.FMLClientSetupEvent event) { + var extensions = extensions(); + net.modcrafters.minecraft.primalpottery.item.ModItems.POTTERY.renderProperties = extensions; + net.modcrafters.minecraft.primalpottery.item.ModItems.RAW_POTTERY.renderProperties = extensions; + } + *///?} + + //? if <26.1 { + /*private static void colors(net.neoforged.neoforge.client.event.RegisterColorHandlersEvent.Item event) { + event.register( + (stack, tintIndex) -> { + Integer color = net.modcrafters.minecraft.primalpottery.item.PotteryItem.dyeColor( + net.modcrafters.minecraft.primalpottery.item.PotteryItem.dye(stack) + ); + return color == null ? -1 : 0xFF000000 | color; + }, + net.modcrafters.minecraft.primalpottery.item.ModItems.RAW_POTTERY, + net.modcrafters.minecraft.primalpottery.item.ModItems.POTTERY + ); + } + *///?} + private static void renderers(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.WHEEL, context -> new WheelRenderer()); event.registerBlockEntityRenderer(ModBlockEntities.POTTERY, context -> new PotteryRenderer()); diff --git a/neoforge/versions/1.20.6/src/main/resources/META-INF/accesstransformer.cfg b/neoforge/versions/1.20.6/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 0000000..56f65e4 --- /dev/null +++ b/neoforge/versions/1.20.6/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.world.item.Item renderProperties diff --git a/neoforge/versions/26.3/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/PotteryGameTests.java b/neoforge/versions/26.3/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/PotteryGameTests.java index 27dd943..4610deb 100644 --- a/neoforge/versions/26.3/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/PotteryGameTests.java +++ b/neoforge/versions/26.3/src/main/java/net/modcrafters/minecraft/primalpottery/neoforge/PotteryGameTests.java @@ -22,9 +22,14 @@ import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.DyeColor; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Item; import net.minecraft.world.item.Items; +import net.minecraft.world.item.crafting.AbstractCookingRecipe; +import net.minecraft.world.item.crafting.CraftingRecipe; +import net.minecraft.world.item.crafting.RecipeBookCategories; +import net.minecraft.world.item.crafting.SingleRecipeInput; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.item.crafting.CraftingInput; @@ -47,17 +52,21 @@ import net.modcrafters.minecraft.primalpottery.block.ModBlocks; import net.modcrafters.minecraft.primalpottery.block.PotteryBlock; import net.modcrafters.minecraft.primalpottery.block.PotteryBlockEntity; +import net.modcrafters.minecraft.primalpottery.block.PotteryWheelBlock; import net.modcrafters.minecraft.primalpottery.block.PotteryWheelBlockEntity; import net.modcrafters.minecraft.primalpottery.item.ModItems; import net.modcrafters.minecraft.primalpottery.item.PotteryItem; +import net.modcrafters.minecraft.primalpottery.item.RawPotteryItem; import net.modcrafters.minecraft.primalpottery.item.TemplateItem; +import net.modcrafters.minecraft.primalpottery.recipe.FirePotteryRecipe; +import net.modcrafters.minecraft.primalpottery.recipe.PaintPotteryRecipe; import net.modcrafters.minecraft.primalpottery.network.LidSync; import net.modcrafters.minecraft.primalpottery.network.PlacedLidSync; import net.modcrafters.minecraft.primalpottery.network.TemplateSync; import net.modcrafters.minecraft.primalpottery.pottery.Nbt; +import net.modcrafters.minecraft.primalpottery.pottery.Placement; import net.modcrafters.minecraft.primalpottery.pottery.Pottery; import net.modcrafters.minecraft.primalpottery.pottery.PotteryDisc; -import net.modcrafters.minecraft.primalpottery.pottery.PotteryType; import net.modcrafters.minecraft.primalpottery.pottery.VesselContents; import net.neoforged.neoforge.event.RegisterGameTestsEvent; @@ -87,15 +96,16 @@ public static void registerTests(RegisterGameTestsEvent event) { private static void wheel(GameTestHelper helper) { BlockPos wheelPos = new BlockPos(1, 2, 1); - helper.setBlock(wheelPos, ModBlocks.WHEEL); Player player = mockPlayer(helper); + wheelPlacement(helper, player, wheelPos); PotteryWheelBlockEntity wheel = helper.getBlockEntity(wheelPos, PotteryWheelBlockEntity.class); checkTemplateCycle(helper, player); + installedTemplate(helper, player, wheelPos); aim(player, partCenter(helper, wheelPos, PotteryWheelBlockEntity.BIG_WHEEL)); player.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); - helper.useBlock(wheelPos, player); + helper.useBlock(wheelPos.above(), player); helper.assertValueEqual(wheel.spinTicks(), 24, "spin ticks"); ItemStack clay = new ItemStack(Items.CLAY_BALL); @@ -117,11 +127,12 @@ private static void wheel(GameTestHelper helper) { helper.assertValueEqual(tracked.angle(), wheel.angle(), "wheel angle in tracking snapshot"); helper.assertValueEqual(tracked.clayProgress(), wheel.clayProgress(), "saved shaping progress"); + float progress = wheel.clayProgress(); helper.useBlock(wheelPos, player); helper.assertFalse(wheel.hasClay(), "clay was not taken off"); - ItemStack shaped = potteryItem(helper); - helper.assertValueEqual(PotteryItem.read(shaped).discs().size(), 5, "wheel discs"); - helper.assertValueEqual(PotteryItem.read(shaped).classify().type(), PotteryType.LUMP, "wheel pottery"); + helper.assertTrue(wheel.start() == null, "start was not cleared"); + ItemStack shaped = rawItem(helper); + helper.assertValueEqual(PotteryItem.read(shaped).discs(), Pottery.shaped(null, Pottery.formed(null), progress).discs(), "early raw profile"); BlockPos floor = new BlockPos(3, 1, 1); helper.setBlock(floor, Blocks.STONE); @@ -136,6 +147,20 @@ private static void wheel(GameTestHelper helper) { false ) )); + helper.assertBlockNotPresent(ModBlocks.POTTERY, floor.above()); + + ItemStack fired = PotteryItem.stack(Pottery.wheelShape()); + player.setItemInHand(InteractionHand.MAIN_HAND, fired.copy()); + fired.useOn(new UseOnContext( + player, + InteractionHand.MAIN_HAND, + new BlockHitResult( + helper.absoluteVec(new Vec3(3.2, 2.0, 1.2)), + net.minecraft.core.Direction.UP, + helper.absolutePos(floor), + false + ) + )); helper.assertBlockPresent(ModBlocks.POTTERY, floor.above()); PotteryBlockEntity pots = helper.getBlockEntity(floor.above(), PotteryBlockEntity.class); helper.assertTrue(pots.get(0) != null, "pot was not placed"); @@ -146,24 +171,75 @@ private static void wheel(GameTestHelper helper) { player.setShiftKeyDown(false); helper.assertBlockNotPresent(ModBlocks.POTTERY, floor.above()); potteryItem(helper); + forming(helper, player, wheelPos); ((ServerPlayer) player).setGameMode(GameType.SURVIVAL); helper.assertFalse(player.preventsBlockDrops(), "survival wheel drops disabled"); BlockPos absoluteWheel = helper.absolutePos(wheelPos); + Pottery kept = new Pottery(List.of(new PotteryDisc(1, 4, 2))); for (BlockPos broken : List.of(absoluteWheel, absoluteWheel.above())) { helper.setBlock(wheelPos, ModBlocks.WHEEL); - helper.getBlockEntity(wheelPos, PotteryWheelBlockEntity.class).tick(); + PotteryWheelBlockEntity placed = helper.getBlockEntity(wheelPos, PotteryWheelBlockEntity.class); + placed.tick(); + placed.install(kept); + helper.assertBlockPresent(ModBlocks.WHEEL, wheelPos.above()); ((ServerPlayer) player).gameMode.destroyBlock(broken); helper.assertBlockNotPresent(ModBlocks.WHEEL, wheelPos); helper.assertBlockNotPresent(ModBlocks.WHEEL, wheelPos.above()); var drops = helper.getLevel().getEntitiesOfClass(ItemEntity.class, new AABB(absoluteWheel).inflate(2), item -> item.getItem().is(ModItems.WHEEL)); - helper.assertValueEqual(drops.stream().mapToInt(item -> item.getItem().getCount()).sum(), 1, "wheel drops from " + broken); + helper.assertValueEqual(drops.stream().mapToInt(item -> item.getItem().getCount()).sum(), 1, "wheel drops"); + var templates = helper.getLevel().getEntitiesOfClass(ItemEntity.class, new AABB(absoluteWheel).inflate(2), + item -> item.getItem().is(ModItems.BLUEPRINT)); + helper.assertValueEqual(templates.stream().mapToInt(item -> item.getItem().getCount()).sum(), 1, "template drops"); + helper.assertValueEqual(TemplateItem.read(templates.get(0).getItem()).discs(), kept.discs(), "dropped template"); drops.forEach(ItemEntity::discard); + templates.forEach(ItemEntity::discard); } helper.succeed(); } + private static void wheelPlacement(GameTestHelper helper, Player player, BlockPos base) { + ((ServerPlayer) player).setGameMode(GameType.SURVIVAL); + BlockPos floor = base.below(); + BlockPos upper = base.above(); + helper.setBlock(floor, Blocks.STONE); + helper.setBlock(upper, Blocks.STONE); + holdStack(player, new ItemStack(ModItems.WHEEL)); + useItemOn(helper, player, floor, topPoint(floor, 0), Direction.UP); + helper.assertBlockNotPresent(ModBlocks.WHEEL, base); + helper.assertBlockPresent(Blocks.STONE, upper); + helper.assertValueEqual(player.getMainHandItem().getCount(), 1, "wheel consumed under a low ceiling"); + + helper.setBlock(upper, Blocks.AIR); + useItemOn(helper, player, floor, topPoint(floor, 0), Direction.UP); + helper.assertBlockProperty(base, PotteryWheelBlock.HALF, PotteryWheelBlock.LOWER); + helper.assertBlockProperty(upper, PotteryWheelBlock.HALF, PotteryWheelBlock.UPPER); + helper.assertTrue(player.getMainHandItem().isEmpty(), "wheel placement did not consume the item"); + + AABB bounds = new AABB(0, 0, 0, 1, 1.5, 1).move(helper.absolutePos(base)); + for (BlockPos part : List.of(base, upper)) { + BlockPos absolute = helper.absolutePos(part); + BlockHitResult hit = new BlockHitResult(Vec3.atCenterOf(absolute), Direction.NORTH, absolute, false); + helper.assertValueEqual(Placement.outline(helper.getLevel(), player, hit), bounds, "whole wheel outline from " + part); + AABB collision = helper.getBlockState(part).getCollisionShape(helper.getLevel(), absolute).bounds(); + helper.assertValueEqual(collision, new AABB(0, 0, 0, 1, part.equals(base) ? 1 : 0.5, 1), "collision at " + part); + } + + BlockPos neighbor = upper.west(); + helper.setBlock(neighbor, Blocks.STONE); + Vec3 side = Vec3.atCenterOf(neighbor).add(0.5, 0, 0); + player.setShiftKeyDown(true); + for (Item item : List.of(Items.STONE, Items.OAK_SLAB)) { + player.setItemInHand(InteractionHand.MAIN_HAND, new ItemStack(item)); + useItemOn(helper, player, neighbor, side, Direction.EAST); + helper.assertBlockProperty(upper, PotteryWheelBlock.HALF, PotteryWheelBlock.UPPER); + helper.assertValueEqual(player.getMainHandItem().getCount(), 1, "side placement consumed " + item); + } + helper.setBlock(neighbor, Blocks.AIR); + holdStack(player, ItemStack.EMPTY); + } + private static void checkTemplateCycle(GameTestHelper helper, Player player) { ItemStack raw = new ItemStack(ModItems.RAW_TEMPLATE); player.setItemInHand(InteractionHand.MAIN_HAND, raw); @@ -200,6 +276,317 @@ ItemStack.EMPTY, new ItemStack(Items.STONE_SLAB), ItemStack.EMPTY, CraftingInput blankInput = CraftingInput.of(1, 1, List.of(reset)); helper.assertTrue(recipes.getRecipeFor(RecipeType.CRAFTING, blankInput, helper.getLevel()).isEmpty(), "raw templates should not match the reset recipe"); + + ItemStack stacked = new ItemStack(ModItems.RAW_TEMPLATE, 4); + player.setItemInHand(InteractionHand.MAIN_HAND, stacked); + TemplateSync.apply(player, true, profile); + helper.assertTrue(stacked.is(ModItems.RAW_TEMPLATE), "raw stack was replaced"); + helper.assertValueEqual(stacked.getCount(), 3, "raw stack count"); + helper.assertTrue(TemplateItem.read(stacked) == null, "raw stack gained a profile"); + int savedSlot = -1; + ItemStack saved = ItemStack.EMPTY; + for (int slot = 0; slot < player.getInventory().getContainerSize(); slot++) { + ItemStack item = player.getInventory().getItem(slot); + if (item.is(ModItems.BLUEPRINT)) { + savedSlot = slot; + saved = item; + break; + } + } + helper.assertTrue(savedSlot >= 0, "edited template was not split out"); + helper.assertValueEqual(saved.getCount(), 1, "edited count"); + helper.assertValueEqual(TemplateItem.profile(saved).discs(), profile, "edited profile"); + player.getInventory().setItem(savedSlot, ItemStack.EMPTY); + player.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); + } + + private static void installedTemplate(GameTestHelper helper, Player player, BlockPos wheelPos) { + PotteryWheelBlockEntity wheel = helper.getBlockEntity(wheelPos, PotteryWheelBlockEntity.class); + ((ServerPlayer) player).setGameMode(GameType.CREATIVE); + Pottery first = new Pottery(List.of(new PotteryDisc(1, 4, 2))); + Pottery second = new Pottery(List.of(new PotteryDisc(1, 3, 1))); + + ItemStack held = blueprint(first); + player.setItemInHand(InteractionHand.MAIN_HAND, held); + aim(player, partCenter(helper, wheelPos, PotteryWheelBlockEntity.TABLE)); + helper.useBlock(wheelPos, player); + helper.assertTrue(held.isEmpty(), "creative template was not consumed"); + helper.assertValueEqual(wheel.template().discs(), first.discs(), "installed profile"); + + var saved = wheel.getUpdateTag(helper.getLevel().registryAccess()); + var loaded = new PotteryWheelBlockEntity(helper.absolutePos(wheelPos), wheel.getBlockState()); + loaded.loadCustomOnly(TagValueInput.create(ProblemReporter.DISCARDING, helper.getLevel().registryAccess(), saved)); + helper.assertValueEqual(loaded.template().discs(), first.discs(), "saved template"); + + player.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); + helper.useBlock(wheelPos, player); + helper.assertTrue(wheel.templateItem().isEmpty(), "template stayed in the wheel"); + helper.assertTrue(player.getMainHandItem().is(ModItems.BLUEPRINT), "empty hand did not take the template"); + helper.assertValueEqual(TemplateItem.read(player.getMainHandItem()).discs(), first.discs(), "taken profile"); + + player.setItemInHand(InteractionHand.MAIN_HAND, blueprint(second)); + helper.useBlock(wheelPos, player); + helper.assertTrue(player.getMainHandItem().isEmpty(), "second template was not consumed"); + player.setItemInHand(InteractionHand.MAIN_HAND, blueprint(first)); + helper.useBlock(wheelPos, player); + helper.assertValueEqual(wheel.template().discs(), first.discs(), "swapped profile"); + helper.assertValueEqual(TemplateItem.read(player.getMainHandItem()).discs(), second.discs(), "returned template"); + + ItemStack stack = blueprint(second); + stack.setCount(2); + player.setItemInHand(InteractionHand.MAIN_HAND, stack); + helper.useBlock(wheelPos, player); + helper.assertValueEqual(stack.getCount(), 1, "template stack"); + helper.assertValueEqual(wheel.template().discs(), second.discs(), "stack installed"); + helper.assertValueEqual(TemplateItem.read(stack).discs(), second.discs(), "remaining template"); + ItemStack returned = ItemStack.EMPTY; + for (int slot = 0; slot < player.getInventory().getContainerSize(); slot++) { + ItemStack item = player.getInventory().getItem(slot); + if (item != stack && item.is(ModItems.BLUEPRINT) && first.discs().equals(TemplateItem.read(item).discs())) { + returned = item; + player.getInventory().setItem(slot, ItemStack.EMPTY); + break; + } + } + helper.assertValueEqual(returned.getCount(), 1, "old template from a stack"); + wheel.clearTemplate(); + player.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); + } + + private static ItemStack blueprint(Pottery pottery) { + ItemStack stack = new ItemStack(ModItems.BLUEPRINT); + TemplateItem.write(stack, pottery); + return stack; + } + + private static void forming(GameTestHelper helper, Player player, BlockPos wheelPos) { + discardPottery(helper); + PotteryWheelBlockEntity wheel = helper.getBlockEntity(wheelPos, PotteryWheelBlockEntity.class); + ServerPlayer server = (ServerPlayer) player; + + server.setGameMode(GameType.SURVIVAL); + ItemStack clay = new ItemStack(Items.CLAY_BALL, 2); + offer(helper, player, wheelPos, clay); + helper.assertValueEqual(clay.getCount(), 1, "survival clay count"); + helper.assertTrue(wheel.hasClay(), "survival clay"); + helper.assertValueEqual(wheel.clayProgress(), 0.0F, "survival progress"); + helper.assertTrue(wheel.start() == null, "clay start"); + take(helper, player, wheelPos); + discardPottery(helper); + + server.setGameMode(GameType.CREATIVE); + ItemStack creative = new ItemStack(Items.CLAY_BALL, 2); + offer(helper, player, wheelPos, creative); + helper.assertValueEqual(creative.getCount(), 2, "creative clay count"); + helper.assertTrue(wheel.hasClay() && wheel.start() == null, "creative blank"); + take(helper, player, wheelPos); + discardPottery(helper); + + server.setGameMode(GameType.SURVIVAL); + Pottery returned = new Pottery(List.of(new PotteryDisc(1, 2, 0))); + ItemStack raw = RawPotteryItem.stack(returned); + offer(helper, player, wheelPos, raw); + helper.assertTrue(raw.isEmpty(), "raw pottery was not consumed"); + helper.assertValueEqual(wheel.start().discs(), returned.discs(), "returned start"); + take(helper, player, wheelPos); + discardPottery(helper); + + ItemStack blue = RawPotteryItem.stack(returned, "blue"); + offer(helper, player, wheelPos, blue); + helper.assertFalse(wheel.hasClay(), "dyed raw started the wheel"); + helper.assertValueEqual(blue.getCount(), 1, "dyed raw count"); + ItemStack firedStack = new ItemStack(ModItems.POTTERY); + offer(helper, player, wheelPos, firedStack); + helper.assertFalse(wheel.hasClay(), "fired pottery started the wheel"); + helper.assertValueEqual(firedStack.getCount(), 1, "fired count"); + ItemStack missing = new ItemStack(ModItems.RAW_POTTERY); + offer(helper, player, wheelPos, missing); + helper.assertFalse(wheel.hasClay(), "missing profile started the wheel"); + helper.assertValueEqual(missing.getCount(), 1, "missing profile count"); + ItemStack invalid = RawPotteryItem.stack(new Pottery(List.of(new PotteryDisc(0, 2)))); + offer(helper, player, wheelPos, invalid); + helper.assertFalse(wheel.hasClay(), "invalid profile started the wheel"); + helper.assertValueEqual(invalid.getCount(), 1, "invalid profile count"); + + server.setGameMode(GameType.CREATIVE); + offer(helper, player, wheelPos, new ItemStack(Items.CLAY_BALL)); + shapeFor(helper, player, wheelPos, wheel, 50); + float halfway = wheel.clayProgress(); + take(helper, player, wheelPos); + ItemStack half = rawItem(helper); + helper.assertValueEqual(PotteryItem.read(half).discs(), Pottery.shaped(null, Pottery.formed(null), halfway).discs(), "halfway raw"); + discardPottery(helper); + + offer(helper, player, wheelPos, new ItemStack(Items.CLAY_BALL)); + finish(helper, player, wheelPos, wheel); + float stopped = wheel.angle(); + var finished = wheel.getUpdateTag(helper.getLevel().registryAccess()); + var loaded = new PotteryWheelBlockEntity(helper.absolutePos(wheelPos), wheel.getBlockState()); + loaded.loadCustomOnly(TagValueInput.create(ProblemReporter.DISCARDING, helper.getLevel().registryAccess(), finished)); + helper.assertValueEqual(loaded.spinTicks(), 0, "stopped spin"); + helper.assertValueEqual(loaded.clayProgress(), 1.0F, "stopped progress"); + helper.assertValueEqual(loaded.angle(), stopped, "stopped angle"); + aim(player, partCenter(helper, wheelPos, PotteryWheelBlockEntity.BIG_WHEEL)); + helper.useBlock(wheelPos, player); + helper.assertValueEqual(wheel.spinTicks(), 0, "finished piece kept spinning"); + take(helper, player, wheelPos); + ItemStack done = rawItem(helper); + helper.assertValueEqual(PotteryItem.read(done).discs(), Pottery.formed(null).discs(), "finished raw"); + discardPottery(helper); + aim(player, partCenter(helper, wheelPos, PotteryWheelBlockEntity.BIG_WHEEL)); + helper.useBlock(wheelPos, player); + helper.assertValueEqual(wheel.spinTicks(), 24, "spin after take"); + + offer(helper, player, wheelPos, new ItemStack(Items.CLAY_BALL)); + finish(helper, player, wheelPos, wheel); + CompoundTag saved = wheel.getUpdateTag(helper.getLevel().registryAccess()); + CompoundTag wheelTag = Nbt.compound(saved, "wheel"); + wheelTag.putInt("spin_ticks", 8); + saved.put("wheel", wheelTag); + wheel.loadCustomOnly(TagValueInput.create(ProblemReporter.DISCARDING, helper.getLevel().registryAccess(), saved)); + helper.assertValueEqual(wheel.spinTicks(), 8, "loaded leftover spin"); + float resting = wheel.angle(); + wheel.tick(); + helper.assertValueEqual(wheel.spinTicks(), 0, "leftover spin cleared"); + helper.assertValueEqual(wheel.clayProgress(), 1.0F, "leftover progress"); + helper.assertValueEqual(wheel.angle(), resting, "leftover angle"); + take(helper, player, wheelPos); + discardPottery(helper); + + Pottery template = new Pottery(List.of(new PotteryDisc(1, 4, 2))); + wheel.install(template); + Pottery again = new Pottery(List.of(new PotteryDisc(1, 3, 1))); + server.setGameMode(GameType.SURVIVAL); + ItemStack back = RawPotteryItem.stack(again); + offer(helper, player, wheelPos, back); + helper.assertTrue(back.isEmpty(), "reshape was not consumed"); + helper.assertValueEqual(wheel.start().discs(), again.discs(), "reshape start"); + shapeFor(helper, player, wheelPos, wheel, 50); + float toward = wheel.clayProgress(); + take(helper, player, wheelPos); + helper.assertValueEqual(PotteryItem.read(rawItem(helper)).discs(), Pottery.shaped(again, template, toward).discs(), "reshape drop"); + discardPottery(helper); + offer(helper, player, wheelPos, RawPotteryItem.stack(again)); + finish(helper, player, wheelPos, wheel); + take(helper, player, wheelPos); + helper.assertValueEqual(PotteryItem.read(rawItem(helper)).discs(), template.discs(), "reshape finished"); + discardPottery(helper); + wheel.clearTemplate(); + + var recipes = helper.getLevel().recipeAccess(); + Pottery profile = new Pottery(List.of(new PotteryDisc(1, 2, 0))); + ItemStack unpainted = RawPotteryItem.stack(profile); + CraftingInput grid = CraftingInput.of(2, 1, List.of(unpainted, new ItemStack(Items.DYE.pick(DyeColor.BLUE)))); + var paintHolder = recipes.getRecipeFor(RecipeType.CRAFTING, grid, helper.getLevel()).orElseThrow(); + helper.assertTrue(paintHolder.id().identifier().equals(Ids.of("paint_pottery")), "paint id"); + helper.assertTrue(paintHolder.value() instanceof CraftingRecipe, "paint is not a crafting recipe"); + CraftingRecipe paint = (CraftingRecipe) paintHolder.value(); + helper.assertTrue(paint.isSpecial(), "paint is not special"); + helper.assertTrue(paint.getSerializer() == PaintPotteryRecipe.SERIALIZER, "paint serializer"); + ItemStack painted = paint.assemble(grid); + helper.assertValueEqual(PotteryItem.read(painted).discs(), profile.discs(), "painted discs"); + helper.assertValueEqual(PotteryItem.dye(painted), "blue", "painted dye"); + offer(helper, player, wheelPos, painted); + helper.assertFalse(wheel.hasClay(), "painted raw started the wheel"); + helper.assertValueEqual(painted.getCount(), 1, "painted raw count"); + + var fireHolder = recipes.getRecipeFor(RecipeType.SMELTING, new SingleRecipeInput(painted), helper.getLevel()).orElseThrow(); + helper.assertTrue(recipes.getRecipeFor(RecipeType.SMELTING, new SingleRecipeInput(unpainted), helper.getLevel()).isPresent(), "undyed raw smelts"); + helper.assertTrue(fireHolder.value() instanceof AbstractCookingRecipe, "fire is not a cooking recipe"); + AbstractCookingRecipe fire = (AbstractCookingRecipe) fireHolder.value(); + helper.assertValueEqual(fire.cookingTime(), 200, "cooking time"); + helper.assertValueEqual(fire.experience(), 0.1F, "experience"); + helper.assertTrue(fire.getType() == RecipeType.SMELTING, "fire type"); + helper.assertTrue(fire.recipeBookCategory() == RecipeBookCategories.FURNACE_MISC, "fire book"); + helper.assertTrue(fire.getSerializer() == FirePotteryRecipe.SERIALIZER, "fire serializer"); + helper.assertTrue(recipes.getRecipeFor(RecipeType.SMELTING, new SingleRecipeInput(new ItemStack(ModItems.POTTERY)), helper.getLevel()).isEmpty(), "fired pottery smelts"); + ItemStack smelted = fire.assemble(new SingleRecipeInput(painted)); + helper.assertTrue(smelted.is(ModItems.POTTERY), "smelted item"); + helper.assertValueEqual(PotteryItem.read(smelted).discs(), profile.discs(), "smelted discs"); + helper.assertValueEqual(PotteryItem.dye(smelted), "blue", "smelted dye"); + helper.assertTrue(PotteryItem.contents(smelted).isEmpty(), "smelted contents"); + helper.assertFalse(PotteryItem.lid(smelted), "smelted lid"); + + BlockPos dyeFloor = new BlockPos(4, 1, 3); + helper.setBlock(dyeFloor, Blocks.STONE); + holdStack(player, PotteryItem.stack(profile, VesselContents.EMPTY, true, "blue")); + clickOn(helper, player, dyeFloor, new Vec3(dyeFloor.getX() + 0.2, dyeFloor.getY() + 1, dyeFloor.getZ() + 0.2), Direction.UP); + BlockPos dyedPots = dyeFloor.above(); + helper.assertBlockPresent(ModBlocks.POTTERY, dyedPots); + PotteryBlockEntity dyed = helper.getBlockEntity(dyedPots, PotteryBlockEntity.class); + helper.assertValueEqual(dyed.dye(0), "blue", "placed dye"); + helper.assertTrue(dyed.lid(0), "placed lid"); + CompoundTag kept = dyed.saveCustomOnly(helper.getLevel().registryAccess()); + PotteryBlockEntity reloaded = new PotteryBlockEntity(dyed.getBlockPos(), dyed.getBlockState()); + reloaded.loadCustomOnly(TagValueInput.create(ProblemReporter.DISCARDING, helper.getLevel().registryAccess(), kept)); + helper.assertValueEqual(reloaded.dye(0), "blue", "saved dye"); + clearPottery(helper); + player.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); + player.setShiftKeyDown(true); + useItemOn(helper, player, dyedPots, slotPoint(dyedPots, 0), Direction.UP); + player.setShiftKeyDown(false); + helper.assertValueEqual(PotteryItem.dye(potteryItemNear(helper, dyedPots)), "blue", "picked dye"); + + server.setGameMode(GameType.SURVIVAL); + helper.setBlock(wheelPos, ModBlocks.WHEEL); + PotteryWheelBlockEntity breaking = helper.getBlockEntity(wheelPos, PotteryWheelBlockEntity.class); + breaking.tick(); + offer(helper, player, wheelPos, new ItemStack(Items.CLAY_BALL)); + helper.assertTrue(breaking.hasClay(), "break setup"); + discardPottery(helper); + BlockPos absolute = helper.absolutePos(wheelPos); + ((ServerPlayer) player).gameMode.destroyBlock(absolute); + helper.assertValueEqual(count(helper, absolute, ModItems.RAW_POTTERY), 1, "break raw"); + helper.assertValueEqual(count(helper, absolute, ModItems.WHEEL), 1, "break wheel"); + discardPottery(helper); + server.setGameMode(GameType.SURVIVAL); + } + + private static void offer(GameTestHelper helper, Player player, BlockPos wheelPos, ItemStack stack) { + player.setShiftKeyDown(false); + player.setItemInHand(InteractionHand.MAIN_HAND, stack); + aim(player, partCenter(helper, wheelPos, PotteryWheelBlockEntity.SMALL_WHEEL)); + helper.useBlock(wheelPos, player); + } + + private static void take(GameTestHelper helper, Player player, BlockPos wheelPos) { + aim(player, partCenter(helper, wheelPos, PotteryWheelBlockEntity.SMALL_WHEEL)); + helper.useBlock(wheelPos, player); + } + + private static void shapeFor(GameTestHelper helper, Player player, BlockPos wheelPos, PotteryWheelBlockEntity wheel, int ticks) { + for (int tick = 0; tick < ticks; tick++) { + if (wheel.spinTicks() == 0) { + aim(player, partCenter(helper, wheelPos, PotteryWheelBlockEntity.BIG_WHEEL)); + helper.useBlock(wheelPos, player); + } + wheel.tick(); + } + } + + private static void finish(GameTestHelper helper, Player player, BlockPos wheelPos, PotteryWheelBlockEntity wheel) { + int guard = 0; + while (wheel.clayProgress() < 1.0F && guard++ < 200) { + shapeFor(helper, player, wheelPos, wheel, 1); + } + helper.assertValueEqual(wheel.clayProgress(), 1.0F, "shaping did not finish"); + helper.assertValueEqual(wheel.spinTicks(), 0, "shaping did not stop"); + } + + private static int count(GameTestHelper helper, BlockPos absolute, Item item) { + return helper.getLevel().getEntitiesOfClass(ItemEntity.class, new AABB(absolute).inflate(2), entity -> entity.getItem().is(item)) + .stream() + .mapToInt(entity -> entity.getItem().getCount()) + .sum(); + } + + private static void discardPottery(GameTestHelper helper) { + BlockPos min = helper.absolutePos(BlockPos.ZERO); + BlockPos max = helper.absolutePos(new BlockPos(16, 8, 16)); + AABB box = new AABB(min.getX() - 2, min.getY() - 2, min.getZ() - 2, max.getX() + 2, max.getY() + 2, max.getZ() + 2); + helper.getLevel().getEntitiesOfClass(ItemEntity.class, box, entity -> + entity.getItem().is(ModItems.POTTERY) || entity.getItem().is(ModItems.RAW_POTTERY) || entity.getItem().is(ModItems.WHEEL) + ).forEach(ItemEntity::discard); } private static ItemStack potteryItem(GameTestHelper helper) { @@ -212,6 +599,16 @@ private static ItemStack potteryItem(GameTestHelper helper) { return items.getFirst().getItem(); } + private static ItemStack rawItem(GameTestHelper helper) { + var items = helper.getLevel().getEntitiesOfClass( + ItemEntity.class, + helper.getBounds().inflate(8), + entity -> entity.getItem().is(ModItems.RAW_POTTERY) + ); + helper.assertFalse(items.isEmpty(), "raw pottery was not dropped"); + return items.getFirst().getItem(); + } + /** The batch places other tests nearby, so a bounds-wide search can see their empty pots. */ private static ItemStack potteryItemNear(GameTestHelper helper, BlockPos relative) { AABB box = new AABB(helper.absolutePos(relative)).inflate(2); diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryBlock.java b/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryBlock.java index 9bfc3f3..f212120 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryBlock.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryBlock.java @@ -86,11 +86,12 @@ public static boolean breakFocused(Level level, BlockPos pos, Player player) { Pottery taken = pottery.get(part); VesselContents contents = pottery.contents(part); boolean lid = pottery.lid(part); + String dye = pottery.dye(part); BlockState state = level.getBlockState(pos); pottery.take(part); if (!level.isClientSide()) { if (!player.hasInfiniteMaterials()) { - popResource(level, pos, PotteryItem.stack(taken, contents, lid)); + popResource(level, pos, PotteryItem.stack(taken, contents, lid, dye)); } level.levelEvent(2001, pos, Block.getId(state)); } @@ -149,7 +150,7 @@ public static InteractionResult tryPlace( return InteractionResult.FAIL; } if (!level.isClientSide()) { - if (!pottery.put(target, PotteryItem.read(stack), PotteryItem.contents(stack), PotteryItem.lid(stack))) { + if (!pottery.put(target, PotteryItem.read(stack), PotteryItem.contents(stack), PotteryItem.lid(stack), PotteryItem.dye(stack))) { return InteractionResult.FAIL; } level.playSound(null, pos, SoundType.DECORATED_POT.getPlaceSound(), SoundSource.BLOCKS, 1, 0.8F); @@ -174,9 +175,10 @@ private static InteractionResult interact(Level level, BlockPos pos, Player play Pottery taken = pottery.get(part); VesselContents contents = pottery.contents(part); boolean lid = pottery.lid(part); + String dye = pottery.dye(part); pottery.take(part); level.addFreshEntity(new ItemEntity( - level, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5, PotteryItem.stack(taken, contents, lid) + level, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5, PotteryItem.stack(taken, contents, lid, dye) )); } return InteractionResult.SUCCESS; diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryBlockEntity.java b/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryBlockEntity.java index 77a6652..2d776ca 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryBlockEntity.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryBlockEntity.java @@ -28,6 +28,7 @@ public class PotteryBlockEntity extends BlockEntity { private final Pottery[] slots = new Pottery[SLOTS]; private final VesselContents[] contents = blank(); private final boolean[] lids = new boolean[SLOTS]; + private final String[] dyes = new String[SLOTS]; public PotteryBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.POTTERY, pos, state); @@ -65,6 +66,14 @@ public boolean[] lids() { return Arrays.copyOf(this.lids, this.lids.length); } + public @Nullable String dye(int index) { + return this.dyes[index]; + } + + public String[] dyes() { + return Arrays.copyOf(this.dyes, this.dyes.length); + } + /** Brightest open fluid. A lid, an empty slot, and a dark fluid contribute nothing. */ public int light() { int light = 0; @@ -94,16 +103,22 @@ public void setLid(int index, boolean lid) { } public boolean put(int index, Pottery pottery, VesselContents contents, boolean lid) { + return this.put(index, pottery, contents, lid, null); + } + + public boolean put(int index, Pottery pottery, VesselContents contents, boolean lid, @Nullable String dye) { if (index < 0 || index >= this.slots.length || this.slots[index] != null) { return false; } Pottery[] next = Arrays.copyOf(this.slots, this.slots.length); VesselContents[] nextContents = Arrays.copyOf(this.contents, this.contents.length); boolean[] nextLids = Arrays.copyOf(this.lids, this.lids.length); + String[] nextDyes = Arrays.copyOf(this.dyes, this.dyes.length); next[index] = pottery; nextContents[index] = contents == null || contents.isEmpty() ? VesselContents.EMPTY : contents; nextLids[index] = lid; - this.replace(next, nextContents, nextLids); + nextDyes[index] = dye == null || dye.isEmpty() ? null : dye; + this.replace(next, nextContents, nextLids, nextDyes); return true; } @@ -115,10 +130,12 @@ public boolean put(int index, Pottery pottery, VesselContents contents, boolean Pottery[] next = Arrays.copyOf(this.slots, this.slots.length); VesselContents[] nextContents = Arrays.copyOf(this.contents, this.contents.length); boolean[] nextLids = Arrays.copyOf(this.lids, this.lids.length); + String[] nextDyes = Arrays.copyOf(this.dyes, this.dyes.length); next[index] = null; nextContents[index] = VesselContents.EMPTY; nextLids[index] = false; - this.replace(next, nextContents, nextLids); + nextDyes[index] = null; + this.replace(next, nextContents, nextLids, nextDyes); return taken; } @@ -136,17 +153,18 @@ public void dropAll() { this.worldPosition.getX() + 0.5, this.worldPosition.getY() + 0.5, this.worldPosition.getZ() + 0.5, - PotteryItem.stack(pottery, this.contents[index], this.lids[index]) + PotteryItem.stack(pottery, this.contents[index], this.lids[index], this.dyes[index]) )); } } - private void replace(Pottery[] next, VesselContents[] nextContents, boolean[] nextLids) { + private void replace(Pottery[] next, VesselContents[] nextContents, boolean[] nextLids, String[] nextDyes) { boolean any = false; for (int index = 0; index < next.length; index++) { this.slots[index] = next[index]; this.contents[index] = nextContents[index] == null ? VesselContents.EMPTY : nextContents[index]; this.lids[index] = next[index] != null && nextLids[index]; + this.dyes[index] = next[index] == null ? null : nextDyes[index]; any |= next[index] != null; } if (!any && this.level != null) { @@ -200,6 +218,9 @@ private CompoundTag saveSlots() { if (this.lids[index]) { entry.putBoolean("lid", true); } + if (this.dyes[index] != null && !this.dyes[index].isEmpty()) { + entry.putString("dye", this.dyes[index]); + } } list.add(entry); } @@ -213,6 +234,7 @@ private void loadSlots(CompoundTag tag) { this.slots[index] = null; this.contents[index] = VesselContents.EMPTY; this.lids[index] = false; + this.dyes[index] = null; if (index >= list.size()) { continue; } @@ -222,6 +244,8 @@ private void loadSlots(CompoundTag tag) { this.slots[index] = Pottery.load(pottery); this.contents[index] = VesselContents.read(entry); this.lids[index] = Nbt.getBoolean(entry, "lid"); + String dye = Nbt.getString(entry, "dye"); + this.dyes[index] = dye.isEmpty() ? null : dye; } } } diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryWheelBlock.java b/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryWheelBlock.java index efe709d..043d3aa 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryWheelBlock.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryWheelBlock.java @@ -23,13 +23,15 @@ import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.DoubleBlockHalf; import net.minecraft.world.level.storage.loot.LootParams; +import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; import net.modcrafters.minecraft.primalpottery.item.ModItems; -import net.modcrafters.minecraft.primalpottery.item.TemplateItem; +import net.modcrafters.minecraft.primalpottery.item.RawPotteryItem; +import net.modcrafters.minecraft.primalpottery.pottery.Pottery; import net.modcrafters.minecraft.primalpottery.pottery.PartHits; import net.modcrafters.minecraft.primalpottery.pottery.Placement; import org.jspecify.annotations.Nullable; @@ -38,7 +40,8 @@ public class PotteryWheelBlock extends Block implements EntityBlock { public static final DoubleBlockHalf LOWER = DoubleBlockHalf.LOWER; public static final DoubleBlockHalf UPPER = DoubleBlockHalf.UPPER; public static final net.minecraft.world.level.block.state.properties.EnumProperty HALF = BlockStateProperties.DOUBLE_BLOCK_HALF; - private static final VoxelShape UPPER_SHAPE = Shapes.box(0, 0, 0, 1, PotteryWheelBlockEntity.BLOCK_HEIGHT - 1, 1); + private static final AABB BOUNDS = new AABB(0, 0, 0, 1, 1.5, 1); + private static final VoxelShape UPPER_SHAPE = Shapes.box(0, 0, 0, 1, BOUNDS.maxY - 1, 1); private static boolean removingPartner; public PotteryWheelBlock(Properties properties) { @@ -101,10 +104,11 @@ protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, return state.getValue(HALF) == UPPER ? UPPER_SHAPE : Shapes.block(); } - static void removePartner(Level level, BlockPos pos, BlockState state, boolean drop) { + private static void removePartner(Level level, BlockPos pos, BlockState state, boolean drop) { if (removingPartner) { return; } + releaseContents(level, origin(state, pos), drop); BlockPos other = state.getValue(HALF) == LOWER ? pos.above() : pos.below(); BlockState otherState = level.getBlockState(other); if (!otherState.is(state.getBlock()) || otherState.getValue(HALF) == state.getValue(HALF)) { @@ -131,7 +135,7 @@ static void removePartner(Level level, BlockPos pos, BlockState state, boolean d @Override public @Nullable BlockEntityTicker getTicker(Level level, BlockState state, BlockEntityType type) { - if (type != ModBlockEntities.WHEEL) { + if (state.getValue(HALF) == UPPER || type != ModBlockEntities.WHEEL) { return null; } return (level1, pos, state1, blockEntity) -> ((PotteryWheelBlockEntity) blockEntity).tick(); @@ -142,19 +146,44 @@ public RenderShape getRenderShape(BlockState state) { return RenderShape.INVISIBLE; } + private static void releaseContents(Level level, BlockPos pos, boolean drop) { + if (level.isClientSide() || !(level.getBlockEntity(pos) instanceof PotteryWheelBlockEntity wheel)) { + return; + } + ItemStack forming = wheel.removeForming(); + ItemStack template = wheel.removeTemplate(); + if (!drop) { + return; + } + if (!forming.isEmpty()) { + popResource(level, pos, forming); + } + if (!template.isEmpty()) { + popResource(level, pos, template); + } + } + + private static BlockPos origin(BlockState state, BlockPos pos) { + return state.getValue(HALF) == UPPER ? pos.below() : pos; + } + + public static AABB outline(BlockState state, BlockPos pos) { + return BOUNDS.move(origin(state, pos)); + } + //? if >=1.21.2 { @Override public InteractionResult useItemOn( ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit ) { - return interact(level, origin(state, pos), player, stack); + return interact(level, origin(state, pos), player, hand, stack); } //?} else { /*@Override public net.minecraft.world.ItemInteractionResult useItemOn( ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit ) { - InteractionResult result = interact(level, origin(state, pos), player, stack); + InteractionResult result = interact(level, origin(state, pos), player, hand, stack); if (result.consumesAction()) { return net.minecraft.world.ItemInteractionResult.SUCCESS; } @@ -164,18 +193,13 @@ public net.minecraft.world.ItemInteractionResult useItemOn( @Override public InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) { - ItemStack stack = player.getMainHandItem(); - if (stack.isEmpty()) { - stack = player.getOffhandItem(); - } - return interact(level, origin(state, pos), player, stack); - } - - private static BlockPos origin(BlockState state, BlockPos pos) { - return state.getValue(HALF) == UPPER ? pos.below() : pos; + InteractionHand hand = player.getMainHandItem().isEmpty() && !player.getOffhandItem().isEmpty() + ? InteractionHand.OFF_HAND + : InteractionHand.MAIN_HAND; + return interact(level, origin(state, pos), player, hand, player.getItemInHand(hand)); } - private static InteractionResult interact(Level level, BlockPos pos, Player player, ItemStack stack) { + private static InteractionResult interact(Level level, BlockPos pos, Player player, InteractionHand hand, ItemStack stack) { if (!(level.getBlockEntity(pos) instanceof PotteryWheelBlockEntity wheel)) { return InteractionResult.PASS; } @@ -187,12 +211,10 @@ private static InteractionResult interact(Level level, BlockPos pos, Player play } if (part == PotteryWheelBlockEntity.TABLE) { if (stack.is(ModItems.BLUEPRINT)) { - wheel.install(TemplateItem.profile(stack)); - return InteractionResult.SUCCESS; + return installTemplate(level, player, hand, stack, wheel); } - if (player.isShiftKeyDown() && stack.isEmpty() && !wheel.hasClay() && wheel.template() != null) { - wheel.clearTemplate(); - return InteractionResult.SUCCESS; + if (stack.isEmpty()) { + return takeTemplate(level, player, hand, wheel); } return InteractionResult.PASS; } @@ -203,10 +225,60 @@ private static InteractionResult interact(Level level, BlockPos pos, Player play wheel.takeClay(player); return InteractionResult.SUCCESS; } - if (stack.is(Items.CLAY_BALL)) { - wheel.startClay(); + Pottery start = acceptedStart(stack); + if (start == null && !stack.is(Items.CLAY_BALL)) { + return InteractionResult.PASS; + } + if (!level.isClientSide()) { + wheel.startClay(stack.is(Items.CLAY_BALL) ? null : start); + if (player != null && !player.hasInfiniteMaterials()) { + stack.shrink(1); + } + } + return InteractionResult.SUCCESS; + } + + /** One blueprint moves into the wheel. A blueprint already there comes back to the hand. */ + private static InteractionResult installTemplate( + Level level, Player player, InteractionHand hand, ItemStack stack, PotteryWheelBlockEntity wheel + ) { + if (level.isClientSide()) { return InteractionResult.SUCCESS; } - return InteractionResult.PASS; + ItemStack stored = stack.split(1); + ItemStack previous = wheel.replaceTemplate(stored); + if (previous.isEmpty()) { + if (player.getItemInHand(hand).isEmpty()) { + player.setItemInHand(hand, ItemStack.EMPTY); + } + return InteractionResult.SUCCESS; + } + if (player.getItemInHand(hand).isEmpty()) { + player.setItemInHand(hand, previous); + } else if (!player.getInventory().add(previous)) { + //? if >=26.1 { + player.drop(previous, false, net.minecraft.util.Prediction.SERVER_ONLY); + //?} else + //player.drop(previous, false); + } + return InteractionResult.SUCCESS; + } + + private static InteractionResult takeTemplate(Level level, Player player, InteractionHand hand, PotteryWheelBlockEntity wheel) { + if (wheel.templateItem().isEmpty()) { + return InteractionResult.PASS; + } + if (!level.isClientSide()) { + player.setItemInHand(hand, wheel.removeTemplate()); + } + return InteractionResult.SUCCESS; + } + + /** A clay ball, or the profile of undyed raw pottery. Null when the stack cannot start a piece. */ + private static @Nullable Pottery acceptedStart(ItemStack stack) { + if (stack.is(Items.CLAY_BALL)) { + return null; + } + return RawPotteryItem.wheelProfile(stack); } } diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryWheelBlockEntity.java b/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryWheelBlockEntity.java index fbaab3b..662387a 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryWheelBlockEntity.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/block/PotteryWheelBlockEntity.java @@ -8,18 +8,19 @@ import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.Block; -import net.modcrafters.minecraft.primalpottery.item.PotteryItem; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.AABB; +import net.modcrafters.minecraft.primalpottery.item.ModItems; +import net.modcrafters.minecraft.primalpottery.item.RawPotteryItem; +import net.modcrafters.minecraft.primalpottery.item.TemplateItem; import net.modcrafters.minecraft.primalpottery.pottery.Nbt; import net.modcrafters.minecraft.primalpottery.pottery.Pottery; import org.jspecify.annotations.Nullable; public class PotteryWheelBlockEntity extends BlockEntity { - /** Collision height. The drawn wheel stays one block tall. */ - public static final double BLOCK_HEIGHT = 1.5; public static final int TABLE = 0; public static final int BIG_WHEEL = 1; public static final int SMALL_WHEEL = 2; @@ -29,11 +30,14 @@ public class PotteryWheelBlockEntity extends BlockEntity { box(9.5, 26, 9.5, 22.5, 28, 22.5) ); + private boolean checkUpperHalf = true; private int spinTicks; private double angle; private boolean hasClay; private float clayProgress; - private Pottery template; + /** The one blueprint sitting in the wheel. Empty when none is installed. */ + private ItemStack installed = ItemStack.EMPTY; + private Pottery start; public PotteryWheelBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.WHEEL, pos, state); @@ -52,40 +56,84 @@ public static AABB box(double minX, double minY, double minZ, double maxX, doubl public void spin() { if (this.level != null && !this.level.isClientSide()) { + if (this.hasClay && this.clayProgress >= 1) { + return; + } this.spinTicks += 24; this.sync(); } } public void install(Pottery pottery) { - this.template = pottery; + ItemStack stack = new ItemStack(ModItems.BLUEPRINT); + TemplateItem.write(stack, pottery); + this.installed = stack; this.sync(); } - public void clearTemplate() { - this.template = null; + /** Puts one blueprint in the slot and returns whatever was there. */ + public ItemStack replaceTemplate(ItemStack stack) { + ItemStack previous = this.installed; + this.installed = stack; + this.sync(); + return previous; + } + + public ItemStack removeTemplate() { + ItemStack previous = this.installed; + this.installed = ItemStack.EMPTY; this.sync(); + return previous; } - public void startClay() { + public void clearTemplate() { + this.removeTemplate(); + } + + public ItemStack templateItem() { + return this.installed; + } + + public void startClay(@Nullable Pottery start) { this.hasClay = true; this.clayProgress = 0; + this.start = start; this.sync(); } public void takeClay(Player player) { - this.hasClay = false; - this.clayProgress = 0; - if (this.level != null && !this.level.isClientSide()) { - double x = player.getX(); - double y = player.getY(); - double z = player.getZ(); - ItemEntity drop = new ItemEntity(this.level, x, y, z, PotteryItem.stack(this.formed())); - this.level.addFreshEntity(drop); + boolean present = this.hasClay; + ItemStack drop = present ? this.formingStack() : ItemStack.EMPTY; + this.clearForming(); + if (present && this.level != null && !this.level.isClientSide()) { + this.level.addFreshEntity(new ItemEntity(this.level, player.getX(), player.getY(), player.getZ(), drop)); } this.sync(); } + /** The raw stack {@link #takeClay} would drop, then clears the clay. Empty when the wheel is bare. */ + public ItemStack removeForming() { + if (!this.hasClay) { + return ItemStack.EMPTY; + } + ItemStack drop = this.formingStack(); + this.clearForming(); + return drop; + } + + private ItemStack formingStack() { + Pottery shape = this.clayProgress >= 1 + ? this.formed() + : Pottery.shaped(this.start, this.formed(), this.clayProgress); + return RawPotteryItem.stack(shape); + } + + private void clearForming() { + this.hasClay = false; + this.clayProgress = 0; + this.start = null; + } + public int spinTicks() { return this.spinTicks; } @@ -99,19 +147,48 @@ public float clayProgress() { } public @Nullable Pottery template() { - return this.template; + return this.installed.isEmpty() ? null : TemplateItem.profile(this.installed); + } + + public @Nullable Pottery start() { + return this.start; } public Pottery formed() { - return Pottery.formed(this.template); + return Pottery.formed(this.template()); } public float angle() { return (float) this.angle; } + // Wheels saved by the single-block version may still need their upper half. + private void restoreUpperHalf() { + if (this.level == null || this.level.isClientSide()) { + this.checkUpperHalf = this.level == null; + return; + } + BlockPos above = this.worldPosition.above(); + BlockState upper = this.getBlockState().setValue(PotteryWheelBlock.HALF, PotteryWheelBlock.UPPER); + BlockState existing = this.level.getBlockState(above); + if (existing == upper || this.level.isOutsideBuildHeight(above)) { + this.checkUpperHalf = false; + } else if (existing.isAir()) { + this.checkUpperHalf = !this.level.setBlock(above, upper, Block.UPDATE_ALL); + } + } + public void tick() { - this.keepHead(); + if (this.checkUpperHalf) { + this.restoreUpperHalf(); + } + if (this.hasClay && this.clayProgress >= 1 && this.spinTicks > 0) { + this.spinTicks = 0; + if (this.level != null && !this.level.isClientSide()) { + this.sync(); + } + return; + } if (this.spinTicks <= 0) { this.spinTicks = 0; return; @@ -121,12 +198,18 @@ public void tick() { while (this.angle > Math.PI * 2) { this.angle -= Math.PI * 2; } + boolean finished = false; if (this.hasClay && this.clayProgress < 1) { this.clayProgress = Math.min(1, this.clayProgress + 0.01F); + if (this.clayProgress >= 1) { + this.clayProgress = 1; + this.spinTicks = 0; + finished = true; + } } // Clients animate between server snapshots. Persist the authoritative tick and settle all clients at rest. if (this.level != null && !this.level.isClientSide()) { - if (this.spinTicks == 0) { + if (finished || this.spinTicks == 0) { this.sync(); } else { this.setChanged(); @@ -166,8 +249,9 @@ private CompoundTag saveWheel() { tag.putDouble("angle", this.angle); tag.putBoolean("clay", this.hasClay); tag.putFloat("progress", this.clayProgress); - if (this.template != null) { - tag.put("template", this.template.save()); + this.saveTemplate(tag); + if (this.start != null) { + tag.put("start", this.start.save()); } return tag; } @@ -178,22 +262,33 @@ private void loadWheel(CompoundTag tag) { this.angle = Double.isFinite(savedAngle) ? savedAngle % (Math.PI * 2) : 0; this.hasClay = Nbt.getBoolean(tag, "clay"); this.clayProgress = Nbt.getFloat(tag, "progress"); - CompoundTag template = Nbt.compound(tag, "template"); - this.template = template.isEmpty() ? null : Pottery.load(template); + this.loadTemplate(tag); + CompoundTag start = Nbt.compound(tag, "start"); + this.start = start.isEmpty() ? null : Pottery.load(start); } - private void keepHead() { - if (this.level == null || this.level.isClientSide()) { + private void saveTemplate(CompoundTag tag) { + if (this.installed.isEmpty()) { return; } - BlockState state = this.getBlockState(); - if (!state.is(ModBlocks.WHEEL) || state.getValue(PotteryWheelBlock.HALF) != net.minecraft.world.level.block.state.properties.DoubleBlockHalf.LOWER) { + tag.putBoolean("has_template", true); + Pottery saved = TemplateItem.read(this.installed); + if (saved != null) { + tag.put("template", saved.save()); + } + } + + private void loadTemplate(CompoundTag tag) { + CompoundTag saved = Nbt.compound(tag, "template"); + if (saved.isEmpty() && !Nbt.getBoolean(tag, "has_template")) { + this.installed = ItemStack.EMPTY; return; } - BlockPos above = this.worldPosition.above(); - if (this.level.getBlockState(above).isAir()) { - this.level.setBlock(above, state.setValue(PotteryWheelBlock.HALF, net.minecraft.world.level.block.state.properties.DoubleBlockHalf.UPPER), Block.UPDATE_ALL); + ItemStack stack = new ItemStack(ModItems.BLUEPRINT); + if (!saved.isEmpty()) { + TemplateItem.write(stack, Pottery.load(saved)); } + this.installed = stack; } private void sync() { diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/Mesh.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/Mesh.java index 128e693..3ea857c 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/client/Mesh.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/Mesh.java @@ -83,6 +83,37 @@ public void add(Mesh other) { this.quads.addAll(other.quads); } + /** Copies {@code source} spun around the wheel's centre by {@code radians}. */ + public void addSpun(Mesh source, double radians) { + double cos = Math.cos(radians); + double sin = Math.sin(radians); + for (Quad quad : source.quads) { + double nx = quad.nx * cos - quad.nz * sin; + double nz = quad.nx * sin + quad.nz * cos; + Quad spun = new Quad((float) nx, quad.ny, (float) nz, quad.texture); + for (int vertex = 0; vertex < 4; vertex++) { + double dx = quad.x[vertex] - 16; + double dz = quad.z[vertex] - 16; + spun.x[vertex] = (float) (16 + dx * cos - dz * sin); + spun.y[vertex] = quad.y[vertex]; + spun.z[vertex] = (float) (16 + dx * sin + dz * cos); + spun.u[vertex] = quad.u[vertex]; + spun.v[vertex] = quad.v[vertex]; + } + this.quads.add(spun); + } + } + + /** One upright profile on the middle of each table edge, standing perpendicular to that edge. */ + public void edgeBoards(double back, double base, double depth, List discs, Texture texture) { + Mesh board = new Mesh(); + board.board(back, base, 16 - depth / 2.0, depth, discs, texture); + board.fitSprite(); + for (int edge = 0; edge < 4; edge++) { + this.addSpun(board, edge * Math.PI / 2.0); + } + } + public void vessel(double centerX, double base, double centerZ, List discs, Texture texture) { List rising = fromTheBottom(discs); double y = base; @@ -300,13 +331,16 @@ public void fitSprite() { float spanY = span(minY, maxY); float spanZ = span(minZ, maxZ); for (Quad quad : this.quads) { + float ax = Math.abs(quad.nx); + float ay = Math.abs(quad.ny); + float az = Math.abs(quad.nz); for (int vertex = 0; vertex < 4; vertex++) { - if (quad.nx != 0) { - quad.u[vertex] = unit(quad.z[vertex], minZ, spanZ); - quad.v[vertex] = unit(quad.y[vertex], minY, spanY); - } else if (quad.ny != 0) { + if (ay >= ax && ay >= az) { quad.u[vertex] = unit(quad.x[vertex], minX, spanX); quad.v[vertex] = unit(quad.z[vertex], minZ, spanZ); + } else if (ax >= az) { + quad.u[vertex] = unit(quad.z[vertex], minZ, spanZ); + quad.v[vertex] = unit(quad.y[vertex], minY, spanY); } else { quad.u[vertex] = unit(quad.x[vertex], minX, spanX); quad.v[vertex] = unit(quad.y[vertex], minY, spanY); @@ -345,7 +379,9 @@ private void ledge(double centerX, double centerZ, double y, double lower, doubl if (inner <= 0) { this.cap(centerX, centerZ, y, outer, texture, up); } else { - this.ring(centerX, centerZ, y, outer, inner, texture, up); + // A pure up or down normal is fully lit or fully shadowed, so a radius step + // painted a band that did not match the layer beside it. Share the wall's shade. + this.ring(centerX, centerZ, y, outer, inner, texture, up, true, !hole); } } @@ -378,10 +414,24 @@ private void wall( double z1 = centerZ + radius * Math.sin(a1); double top = bottom + height; Direction facing = facing(x0, z0, x1, z1, centerX, centerZ, outward); + double nx = Math.cos(a0) + Math.cos(a1); + double nz = Math.sin(a0) + Math.sin(a1); + double len = Math.hypot(nx, nz); + if (len < 1.0e-8) { + nx = 1; + nz = 0; + len = 1; + } + if (!outward) { + nx = -nx; + nz = -nz; + } + nx /= len; + nz /= len; if (outward) { - this.face(x1, bottom, z1, x0, bottom, z0, x0, top, z0, x1, top, z1, facing, texture); + this.face(x1, bottom, z1, x0, bottom, z0, x0, top, z0, x1, top, z1, facing, nx, 0, nz, texture); } else { - this.face(x0, bottom, z0, x1, bottom, z1, x1, top, z1, x0, top, z0, facing, texture); + this.face(x0, bottom, z0, x1, bottom, z1, x1, top, z1, x0, top, z0, facing, nx, 0, nz, texture); } } @@ -408,6 +458,12 @@ private void cap(double centerX, double centerZ, double y, double radius, Textur } private void ring(double centerX, double centerZ, double y, double outer, double inner, Texture texture, boolean up) { + this.ring(centerX, centerZ, y, outer, inner, texture, up, false, true); + } + + private void ring( + double centerX, double centerZ, double y, double outer, double inner, Texture texture, boolean up, boolean matchWall, boolean outward + ) { double start = Math.PI / 8.0; double step = Math.PI / 4.0; Direction facing = up ? Direction.UP : Direction.DOWN; @@ -422,10 +478,22 @@ private void ring(double centerX, double centerZ, double y, double outer, double double iz0 = centerZ + inner * Math.sin(a0); double ix1 = centerX + inner * Math.cos(a1); double iz1 = centerZ + inner * Math.sin(a1); + double nx = 0; + double ny = up ? 1 : -1; + double nz = 0; + if (matchWall) { + nx = Math.cos(a0) + Math.cos(a1); + nz = Math.sin(a0) + Math.sin(a1); + double len = Math.hypot(nx, nz); + double sign = outward ? 1.0 : -1.0; + nx = sign * nx / len; + ny = 0; + nz = sign * nz / len; + } if (up) { - this.face(ox0, y, oz0, ix0, y, iz0, ix1, y, iz1, ox1, y, oz1, facing, texture); + this.face(ox0, y, oz0, ix0, y, iz0, ix1, y, iz1, ox1, y, oz1, facing, nx, ny, nz, texture); } else { - this.face(ox0, y, oz0, ox1, y, oz1, ix1, y, iz1, ix0, y, iz0, facing, texture); + this.face(ox0, y, oz0, ox1, y, oz1, ix1, y, iz1, ix0, y, iz0, facing, nx, ny, nz, texture); } } } @@ -451,7 +519,21 @@ private void face( Direction facing, Texture texture ) { - Quad quad = new Quad(facing.getStepX(), facing.getStepY(), facing.getStepZ(), texture); + this.face(x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3, facing, facing.getStepX(), facing.getStepY(), facing.getStepZ(), texture); + } + + private void face( + double x0, double y0, double z0, + double x1, double y1, double z1, + double x2, double y2, double z2, + double x3, double y3, double z3, + Direction facing, + double nx, + double ny, + double nz, + Texture texture + ) { + Quad quad = new Quad((float) nx, (float) ny, (float) nz, texture); put(quad, 0, x0, y0, z0, facing); put(quad, 1, x1, y1, z1, facing); put(quad, 2, x2, y2, z2, facing); diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryItemMesh.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryItemMesh.java new file mode 100644 index 0000000..1ac5896 --- /dev/null +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryItemMesh.java @@ -0,0 +1,78 @@ +package net.modcrafters.minecraft.primalpottery.client; + +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import java.util.List; +import net.minecraft.resources.Identifier; +import net.minecraft.world.item.ItemStack; +import net.modcrafters.minecraft.primalpottery.item.ModItems; +import net.modcrafters.minecraft.primalpottery.item.PotteryItem; +import net.modcrafters.minecraft.primalpottery.pottery.Pottery; +import net.modcrafters.minecraft.primalpottery.pottery.PotteryDisc; +import org.jspecify.annotations.Nullable; + +/** + * The thrown profile, scaled to fill an item slot the way the generic vase does. + * Mesh units are 32 per block. The pot stays centered, with its base on the ground. + */ +public final class PotteryItemMesh { + private static final double SLOT_WIDTH = 10.0 / 16.0; + private static final double SLOT_HEIGHT = 12.5 / 16.0; + + private PotteryItemMesh() { + } + + public static Prepared prepare(ItemStack stack) { + boolean raw = stack.is(ModItems.RAW_POTTERY); + Pottery pottery = PotteryItem.read(stack); + boolean closed = !raw && PotteryItem.lid(stack); + return prepare(pottery.discs(), closed, raw, PotteryItem.dye(stack)); + } + + public static Prepared prepare(List discs, boolean closed, boolean raw, @Nullable String dye) { + Mesh vessel = new Mesh(); + vessel.vessel(0, 0, 0, discs, Mesh.Texture.CLAY); + double height = 0; + double radius = 0; + for (PotteryDisc disc : discs) { + height += disc.height(); + radius = Math.max(radius, disc.radius()); + } + Mesh lid = null; + if (closed) { + Pottery.Cover cover = new Pottery(discs).cover(); + if (cover != null) { + lid = new Mesh(); + lid.cover(0, 0, 0, cover, Mesh.Texture.CLAY); + height += cover.layer() + Pottery.PIXEL * 2; + radius = Math.max(radius, cover.hole() + Pottery.PIXEL / 2.0); + } + } + vessel.fitSprite(); + if (lid != null) { + lid.fitSprite(); + } + Identifier texture = raw && PotteryItem.dyeColor(dye) == null ? Sprites.rawTexture() : Sprites.clayTexture(dye); + return new Prepared(vessel, lid, scale(radius, height), texture, Sprites.dyeTint(dye)); + } + + /** Mesh units to block units, keeping the profile inside a 10 by 12.5 pixel icon. */ + public static float scale(double radius, double height) { + double diameter = Math.max(radius * 2.0, 1.0e-3); + double tall = Math.max(height, 1.0e-3); + return (float) Math.min(SLOT_WIDTH / diameter, SLOT_HEIGHT / tall); + } + + public static void draw(ItemStack stack, PoseStack pose, VertexConsumer buffer, int light, boolean shade) { + prepare(stack).draw(pose, buffer, light, shade); + } + + public record Prepared(@Nullable Mesh vessel, @Nullable Mesh lid, float scale, Identifier texture, int color) { + public void draw(PoseStack pose, VertexConsumer buffer, int light, boolean shade) { + pose.pushPose(); + pose.scale(this.scale, this.scale, this.scale); + Sprites.draw(this.vessel, this.lid, buffer, pose.last(), light, this.texture, this.color, shade); + pose.popPose(); + } + } +} diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryItemRenderer.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryItemRenderer.java new file mode 100644 index 0000000..35c2127 --- /dev/null +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryItemRenderer.java @@ -0,0 +1,61 @@ +package net.modcrafters.minecraft.primalpottery.client; + +//? if >=26.1 { +final class PotteryItemRenderer { + private PotteryItemRenderer() { + } +} +//?} else { +/*import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.model.geom.EntityModelSet; +import org.joml.Vector3f; +import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.block.model.ItemTransform; +import net.minecraft.client.renderer.block.model.ItemTransforms; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher; +import net.minecraft.world.item.ItemDisplayContext; +import net.minecraft.world.item.ItemStack; + +public final class PotteryItemRenderer extends BlockEntityWithoutLevelRenderer { + private static final ItemTransforms BLOCK = new ItemTransforms( + transform(75, 45, 0, 0, 2.5F, 0, 0.375F, 0.375F, 0.375F), + transform(75, 45, 0, 0, 2.5F, 0, 0.375F, 0.375F, 0.375F), + transform(0, 225, 0, 0, 0, 0, 0.40F, 0.40F, 0.40F), + transform(0, 45, 0, 0, 0, 0, 0.40F, 0.40F, 0.40F), + ItemTransform.NO_TRANSFORM, + transform(30, 225, 0, 0, 0, 0, 0.625F, 0.625F, 0.625F), + transform(0, 0, 0, 0, 3, 0, 0.25F, 0.25F, 0.25F), + transform(0, 0, 0, 0, 0, 0, 0.5F, 0.5F, 0.5F) + ); + + public PotteryItemRenderer(BlockEntityRenderDispatcher dispatcher, EntityModelSet models) { + super(dispatcher, models); + } + + @Override + public void renderByItem( + ItemStack stack, ItemDisplayContext context, PoseStack pose, MultiBufferSource buffers, int light, int overlay + ) { + boolean left = context == ItemDisplayContext.FIRST_PERSON_LEFT_HAND || context == ItemDisplayContext.THIRD_PERSON_LEFT_HAND; + pose.pushPose(); + pose.translate(0.5F, 0.5F, 0.5F); + BLOCK.getTransform(context).apply(left, pose); + pose.translate(0.0F, -0.5F, 0.0F); + var buffer = buffers.getBuffer(RenderType.entitySolid(Sprites.blocks())); + PotteryItemMesh.draw(stack, pose, buffer, light, context == ItemDisplayContext.GUI); + pose.popPose(); + } + + private static ItemTransform transform( + float rx, float ry, float rz, float tx, float ty, float tz, float sx, float sy, float sz + ) { + return new ItemTransform( + new Vector3f(rx, ry, rz), + new Vector3f(tx / 16.0F, ty / 16.0F, tz / 16.0F), + new Vector3f(sx, sy, sz) + ); + } +} +*///?} diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryPreview.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryPreview.java index 035b599..56efcc9 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryPreview.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryPreview.java @@ -30,22 +30,22 @@ private PotteryPreview() { } static void draw(GuiGraphicsExtractor graphics, List discs, int x0, int y0, int x1, int y1) { - draw(graphics, discs, null, x0, y0, x1, y1); + draw(graphics, discs, null, null, x0, y0, x1, y1); } - static void draw(GuiGraphicsExtractor graphics, Pottery pottery, boolean closed, int x0, int y0, int x1, int y1) { + static void draw(GuiGraphicsExtractor graphics, Pottery pottery, boolean closed, String dye, int x0, int y0, int x1, int y1) { Pottery.Cover cover = closed ? pottery.cover() : null; - draw(graphics, pottery.discs(), cover, x0, y0, x1, y1); + draw(graphics, pottery.discs(), cover, dye, x0, y0, x1, y1); } - private static void draw(GuiGraphicsExtractor graphics, List discs, Pottery.Cover cover, int x0, int y0, int x1, int y1) { + private static void draw(GuiGraphicsExtractor graphics, List discs, Pottery.Cover cover, String dye, int x0, int y0, int x1, int y1) { Model model = model(discs, x1 - x0, y1 - y0, cover); if (model.mesh().quads().isEmpty()) { return; } float spin = (System.currentTimeMillis() / 20) % 360; //? if >=26.1 { - SINK.submit(graphics, new State(model, spin, x0, y0, x1, y1)); + SINK.submit(graphics, new State(model, dye, spin, x0, y0, x1, y1)); //?} else { /*PoseStack pose = graphics.pose(); pose.pushPose(); @@ -54,7 +54,7 @@ private static void draw(GuiGraphicsExtractor graphics, List discs, orient(pose, model, spin); MultiBufferSource.BufferSource buffers = graphics.bufferSource(); RenderType type = RenderType.entitySolid(Sprites.blocks()); - Sprites.draw(model.mesh(), buffers.getBuffer(type), pose.last(), 0xF000F0); + Sprites.draw(model.mesh(), buffers.getBuffer(type), pose.last(), 0xF000F0, Sprites.clayTexture(dye), Sprites.dyeTint(dye)); buffers.endBatch(type); pose.popPose(); *///?} @@ -112,6 +112,7 @@ public interface Sink { public record State( Model model, + @Nullable String dye, float spin, int x0, int y0, @@ -121,9 +122,10 @@ public record State( @Nullable ScreenRectangle scissorArea, @Nullable ScreenRectangle bounds ) implements PictureInPictureRenderState { - private State(Model model, float spin, int x0, int y0, int x1, int y1) { + private State(Model model, @Nullable String dye, float spin, int x0, int y0, int x1, int y1) { this( model, + dye, spin, x0, y0, @@ -149,7 +151,7 @@ protected void renderToTexture(State state, PoseStack pose, SubmitNodeCollector collector.submitCustomGeometry( pose, RenderTypes.entitySolid(Sprites.blocks()), - (entry, buffer) -> Sprites.draw(state.model().mesh(), buffer, entry, 0xF000F0) + (entry, buffer) -> Sprites.draw(state.model().mesh(), buffer, entry, 0xF000F0, Sprites.clayTexture(state.dye()), Sprites.dyeTint(state.dye())) ); } diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryRenderer.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryRenderer.java index 3571014..ef2b2c6 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryRenderer.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryRenderer.java @@ -5,6 +5,7 @@ import java.util.List; import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.resources.Identifier; //? if >=26.1 import net.minecraft.client.renderer.blockentity.state.BlockEntityRenderState; import net.modcrafters.minecraft.primalpottery.block.PotteryBlockEntity; @@ -36,6 +37,7 @@ public void extractRenderState( state.slots = blockEntity.slots(); state.contents = blockEntity.contents(); state.lids = blockEntity.lids(); + state.dyes = blockEntity.dyes(); } @Override @@ -45,23 +47,20 @@ public void submit( net.minecraft.client.renderer.SubmitNodeCollector collector, net.minecraft.client.renderer.state.level.CameraRenderState camera ) { - Mesh mesh = clay(state.slots, state.lids); List draws = fluids(state.slots, state.contents); boolean solidFluid = has(draws, true); boolean translucent = has(draws, false); - if (mesh.quads().isEmpty() && !solidFluid && !translucent) { + if (!occupied(state.slots) && !solidFluid && !translucent) { return; } poseStack.pushPose(); poseStack.scale(1 / 32.0F, 1 / 32.0F, 1 / 32.0F); - if (!mesh.quads().isEmpty() || solidFluid) { + if (occupied(state.slots) || solidFluid) { collector.submitCustomGeometry( poseStack, net.minecraft.client.renderer.rendertype.RenderTypes.entitySolid(Sprites.blocks()), (pose, buffer) -> { - if (!mesh.quads().isEmpty()) { - Sprites.draw(mesh, buffer, pose, state.lightCoords); - } + drawClay(state.slots, state.lids, state.dyes, buffer, pose, state.lightCoords); drawFluids(draws, buffer, pose, state.lightCoords, true); } ); @@ -80,6 +79,7 @@ public static final class State extends BlockEntityRenderState { public Pottery[] slots = new Pottery[PotteryBlockEntity.SLOTS]; public VesselContents[] contents = new VesselContents[PotteryBlockEntity.SLOTS]; public boolean[] lids = new boolean[PotteryBlockEntity.SLOTS]; + public String[] dyes = new String[PotteryBlockEntity.SLOTS]; } //?} else { /*@Override @@ -91,20 +91,17 @@ public void render( int light, int overlay ) { - Mesh mesh = clay(blockEntity.slots(), blockEntity.lids()); List draws = fluids(blockEntity.slots(), blockEntity.contents()); boolean solidFluid = has(draws, true); boolean translucent = has(draws, false); - if (mesh.quads().isEmpty() && !solidFluid && !translucent) { + if (!occupied(blockEntity.slots()) && !solidFluid && !translucent) { return; } poseStack.pushPose(); poseStack.scale(1 / 32.0F, 1 / 32.0F, 1 / 32.0F); - if (!mesh.quads().isEmpty() || solidFluid) { + if (occupied(blockEntity.slots()) || solidFluid) { var buffer = buffers.getBuffer(net.minecraft.client.renderer.RenderType.entitySolid(Sprites.blocks())); - if (!mesh.quads().isEmpty()) { - Sprites.draw(mesh, buffer, poseStack.last(), light); - } + drawClay(blockEntity.slots(), blockEntity.lids(), blockEntity.dyes(), buffer, poseStack.last(), light); drawFluids(draws, buffer, poseStack.last(), light, true); } if (translucent) { @@ -137,27 +134,48 @@ private static void drawFluids( private record FluidDraw(Mesh mesh, TextureAtlasSprite sprite, int color, boolean solid) { } - private static Mesh clay(Pottery[] slots, boolean[] lids) { - Mesh mesh = new Mesh(); + private static boolean occupied(Pottery[] slots) { + if (slots == null) { + return false; + } + for (Pottery pottery : slots) { + if (pottery != null) { + return true; + } + } + return false; + } + + private static void drawClay( + Pottery[] slots, boolean[] lids, String[] dyes, com.mojang.blaze3d.vertex.VertexConsumer buffer, PoseStack.Pose pose, int light + ) { + if (slots == null) { + return; + } for (int index = 0; index < slots.length; index++) { Pottery pottery = slots[index]; if (pottery == null) { continue; } - boolean closed = lids != null && index < lids.length && lids[index]; - Mesh pot = new Mesh(); + String dye = dyes != null && index < dyes.length ? dyes[index] : null; + Identifier texture = Sprites.clayTexture(dye); + int color = Sprites.dyeTint(dye); AABB box = PotteryBlockEntity.box(index); double x = box.getCenter().x * 32; double z = box.getCenter().z * 32; - pot.vessel(x, 0, z, pottery.discs(), Mesh.Texture.CLAY); + Mesh vessel = new Mesh(); + vessel.vessel(x, 0, z, pottery.discs(), Mesh.Texture.CLAY); + vessel.fitSprite(); + boolean closed = lids != null && index < lids.length && lids[index]; Pottery.Cover cover = closed ? pottery.cover() : null; + Mesh lid = null; if (cover != null) { - pot.cover(x, 0, z, cover, Mesh.Texture.CLAY); + lid = new Mesh(); + lid.cover(x, 0, z, cover, Mesh.Texture.CLAY); + lid.fitSprite(); } - pot.fitSprite(); - mesh.add(pot); + Sprites.draw(vessel, lid, buffer, pose, light, texture, color); } - return mesh; } private static List fluids(Pottery[] slots, VesselContents[] contents) { diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryScreen.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryScreen.java index 393eec7..3d77b4c 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryScreen.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotteryScreen.java @@ -34,21 +34,22 @@ public final class PotteryScreen extends Screen { private final int slot; private final Pottery pottery; private final VesselContents contents; + private final @Nullable String dye; private boolean closed; private Button lidButton; private int left; private int top; - public PotteryScreen(boolean mainHand, Pottery pottery, VesselContents contents, boolean closed) { - this(mainHand, null, 0, pottery, contents, closed); + public PotteryScreen(boolean mainHand, Pottery pottery, VesselContents contents, boolean closed, @Nullable String dye) { + this(mainHand, null, 0, pottery, contents, closed, dye); } - public PotteryScreen(BlockPos pos, int slot, Pottery pottery, VesselContents contents, boolean closed) { - this(false, pos, slot, pottery, contents, closed); + public PotteryScreen(BlockPos pos, int slot, Pottery pottery, VesselContents contents, boolean closed, @Nullable String dye) { + this(false, pos, slot, pottery, contents, closed, dye); } private PotteryScreen( - boolean mainHand, @Nullable BlockPos placed, int slot, Pottery pottery, VesselContents contents, boolean closed + boolean mainHand, @Nullable BlockPos placed, int slot, Pottery pottery, VesselContents contents, boolean closed, @Nullable String dye ) { //? if >=26.1 { super(Minecraft.getInstance(), Minecraft.getInstance().font, Component.literal("Pottery")); @@ -59,6 +60,7 @@ private PotteryScreen( this.slot = slot; this.pottery = pottery; this.contents = contents; + this.dye = dye; this.closed = closed; } @@ -68,7 +70,8 @@ public static void open(Player player, InteractionHand hand) { hand == InteractionHand.MAIN_HAND, PotteryItem.read(stack), PotteryItem.contents(stack), - PotteryItem.lid(stack) + PotteryItem.lid(stack), + PotteryItem.dye(stack) )); } @@ -83,7 +86,7 @@ public static void open(Level level, BlockPos pos, int slot) { if (pot == null) { return; } - show(new PotteryScreen(pos, slot, pot, pottery.contents(slot), pottery.lid(slot))); + show(new PotteryScreen(pos, slot, pot, pottery.contents(slot), pottery.lid(slot), pottery.dye(slot))); } private static void show(PotteryScreen screen) { @@ -166,7 +169,7 @@ private void paint(GuiGraphicsExtractor graphics) { } private void drawContents(GuiGraphicsExtractor graphics) { - PotteryPreview.draw(graphics, this.pottery, this.closed, this.left + 8, this.top + 4, this.left + 112, this.top + 36); + PotteryPreview.draw(graphics, this.pottery, this.closed, this.dye, this.left + 8, this.top + 4, this.left + 112, this.top + 36); if (!this.pottery.classify().type().isVessel()) { return; } diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotterySpecialRenderer.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotterySpecialRenderer.java new file mode 100644 index 0000000..894f6eb --- /dev/null +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/PotterySpecialRenderer.java @@ -0,0 +1,139 @@ +package net.modcrafters.minecraft.primalpottery.client; + +//? if >=26.1 { +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.serialization.Codec; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import java.util.List; +import java.util.function.Consumer; +import net.minecraft.client.renderer.SubmitNodeCollector; +import net.minecraft.client.renderer.rendertype.RenderTypes; +import net.minecraft.client.renderer.special.SpecialModelRenderer; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.world.item.ItemStack; +import net.modcrafters.minecraft.primalpottery.item.ModItems; +import net.modcrafters.minecraft.primalpottery.item.PotteryItem; +import net.modcrafters.minecraft.primalpottery.pottery.Pottery; +import net.modcrafters.minecraft.primalpottery.pottery.PotteryDisc; +import net.modcrafters.minecraft.primalpottery.pottery.VesselContents; +import org.joml.Vector3f; +import org.joml.Vector3fc; +import org.jspecify.annotations.Nullable; + +/** Draws the stack's thrown profile. The base model only supplies the block display transform. */ +public final class PotterySpecialRenderer implements SpecialModelRenderer { + /** + * The shelf centers the bounds in the opening. This height reaches from the + * middle of the block down to the plank, so the base rests on the wood. + */ + private static final float SHELF_HEIGHT = 2.0F; + /** Extra size on a shelf. The mesh grows from its base, so it stays on the plank. */ + private static final float SHELF_SCALE = 1.5F; + private final boolean onShelf; + + public PotterySpecialRenderer(boolean onShelf) { + this.onShelf = onShelf; + } + + @Override + public void submit( + @Nullable Icon icon, + PoseStack poseStack, + SubmitNodeCollector collector, + int light, + int overlay, + boolean foil, + int outlineColor + ) { + Icon drawn = icon == null ? Icon.fallback() : icon; + PotteryItemMesh.Prepared prepared = PotteryItemMesh.prepare(drawn.discs(), drawn.closed(), drawn.raw(), drawn.dye()); + float scale = prepared.scale() * (this.onShelf ? SHELF_SCALE : 1.0F); + poseStack.pushPose(); + // Mesh sits on the origin. A block item is centered at 0.5, and the display transform subtracts that. + poseStack.translate(0.5F, 0.0F, 0.5F); + poseStack.scale(scale, scale, scale); + collector.submitCustomGeometry( + poseStack, + RenderTypes.entitySolid(Sprites.blocks()), + (pose, buffer) -> Sprites.draw(prepared.vessel(), prepared.lid(), buffer, pose, light, prepared.texture(), prepared.color()) + ); + drawFluid(drawn, poseStack, collector, light); + poseStack.popPose(); + } + + /** Lava shares the solid pass. Water and other fluids use the translucent pass. */ + private static void drawFluid(Icon icon, PoseStack poseStack, SubmitNodeCollector collector, int light) { + if (icon.contents().isEmpty()) { + return; + } + TextureAtlasSprite sprite = Sprites.fluid(icon.contents().fluid()); + if (sprite == null) { + return; + } + Mesh body = new Mesh(); + body.cavity(0, 0, 0, icon.discs(), icon.contents().amount()); + if (body.quads().isEmpty()) { + return; + } + boolean solid = Sprites.fluidSolid(icon.contents().fluid()); + int color = Sprites.fluidColor(icon.contents().fluid()); + collector.submitCustomGeometry( + poseStack, + solid ? RenderTypes.entitySolid(Sprites.blocks()) : RenderTypes.entityTranslucent(Sprites.blocks()), + (pose, buffer) -> Sprites.drawFluid(body, buffer, pose, light, sprite, color) + ); + } + + @Override + public void getExtents(Consumer output) { + float half = 10.0F / 32.0F; + float top = this.onShelf ? SHELF_HEIGHT : 12.5F / 16.0F; + float min = 0.5F - half; + float max = 0.5F + half; + output.accept(new Vector3f(min, 0.0F, min)); + output.accept(new Vector3f(max, 0.0F, min)); + output.accept(new Vector3f(min, 0.0F, max)); + output.accept(new Vector3f(max, 0.0F, max)); + output.accept(new Vector3f(min, top, min)); + output.accept(new Vector3f(max, top, min)); + output.accept(new Vector3f(min, top, max)); + output.accept(new Vector3f(max, top, max)); + } + + @Override + public Icon extractArgument(ItemStack stack) { + boolean raw = stack.is(ModItems.RAW_POTTERY); + Pottery pottery = PotteryItem.read(stack); + VesselContents contents = raw ? VesselContents.EMPTY : PotteryItem.contents(stack); + return new Icon(List.copyOf(pottery.discs()), !raw && PotteryItem.lid(stack), raw, PotteryItem.dye(stack), contents); + } + + public record Icon(List discs, boolean closed, boolean raw, @Nullable String dye, VesselContents contents) { + static Icon fallback() { + return new Icon(Pottery.display().discs(), false, false, null, VesselContents.EMPTY); + } + } + + public record Unbaked(boolean onShelf) implements SpecialModelRenderer.Unbaked { + public static final MapCodec MAP_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( + Codec.BOOL.optionalFieldOf("on_shelf", false).forGetter(Unbaked::onShelf) + ).apply(instance, Unbaked::new)); + + @Override + public MapCodec type() { + return MAP_CODEC; + } + + @Override + public SpecialModelRenderer bake(SpecialModelRenderer.BakingContext context) { + return new PotterySpecialRenderer(this.onShelf); + } + } +} +//?} else { +/*final class PotterySpecialRenderer { + private PotterySpecialRenderer() { + } +} +*///?} diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/Sprites.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/Sprites.java index 631558e..4d365dc 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/client/Sprites.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/Sprites.java @@ -24,13 +24,43 @@ public static TextureAtlasSprite get(Mesh.Texture texture) { case LOG -> "block/oak_log"; case CLAY -> "block/red_terracotta"; }; - Identifier id = Ids.of("minecraft", path); + return sprite(Ids.of("minecraft", path)); + } + + /** White concrete for a known dye, so a vertex tint stays bright. Fired clay with no dye stays red terracotta. */ + public static Identifier clayTexture(@Nullable String dye) { + if (dye != null && vanillaDye(dye)) { + return Ids.of("minecraft", "block/white_concrete"); + } + return Ids.of("minecraft", "block/red_terracotta"); + } + + /** Unfired clay. The pottery shape uses this instead of terracotta. */ + public static Identifier rawTexture() { + return Ids.of("minecraft", "block/clay"); + } + + /** White when the dye is absent or unknown, so an undyed surface keeps its texture. */ + public static int dyeTint(@Nullable String dye) { + Integer color = net.modcrafters.minecraft.primalpottery.item.PotteryItem.dyeColor(dye); + return color == null ? 0xFFFFFF : color; + } + + public static TextureAtlasSprite sprite(Identifier id) { //? if >=26.1 { return Minecraft.getInstance().getAtlasManager().getAtlasOrThrow(net.minecraft.data.AtlasIds.BLOCKS).getSprite(id); //?} else //return Minecraft.getInstance().getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(id); } + private static boolean vanillaDye(String dye) { + return switch (dye) { + case "white", "orange", "magenta", "light_blue", "yellow", "lime", "pink", "gray", "light_gray", "cyan", + "purple", "blue", "brown", "green", "red", "black" -> true; + default -> false; + }; + } + public static Identifier blocks() { return TextureAtlas.LOCATION_BLOCKS; } @@ -94,14 +124,75 @@ public static void draw(Mesh mesh, VertexConsumer buffer, PoseStack.Pose pose, i } public static void draw(Mesh mesh, VertexConsumer buffer, PoseStack.Pose pose, int light, int alpha) { + write(mesh, null, buffer, pose, light, alpha); + } + + public static void draw(Mesh mesh, VertexConsumer buffer, PoseStack.Pose pose, int light, Identifier texture) { + draw(mesh, null, buffer, pose, light, texture, 0xFFFFFF); + } + + public static void draw(Mesh mesh, VertexConsumer buffer, PoseStack.Pose pose, int light, Identifier texture, int color) { + draw(mesh, null, buffer, pose, light, texture, color); + } + + /** Draws each mesh with one sprite. The meshes are not merged. */ + public static void draw(@Nullable Mesh first, @Nullable Mesh second, VertexConsumer buffer, PoseStack.Pose pose, int light, Identifier texture) { + draw(first, second, buffer, pose, light, texture, 0xFFFFFF); + } + + /** Draws each mesh with one sprite, multiplied by {@code color}. The meshes are not merged. */ + public static void draw(@Nullable Mesh first, @Nullable Mesh second, VertexConsumer buffer, PoseStack.Pose pose, int light, Identifier texture, int color) { + draw(first, second, buffer, pose, light, texture, color, false); + } + + /** + * Same as {@link #draw(Mesh, Mesh, VertexConsumer, PoseStack.Pose, int, Identifier, int)}. + * When {@code shade} is set, each face is darkened the way a block item is, so a flat-lit icon still reads as a solid. + */ + public static void draw(@Nullable Mesh first, @Nullable Mesh second, VertexConsumer buffer, PoseStack.Pose pose, int light, Identifier texture, int color, boolean shade) { + TextureAtlasSprite sprite = sprite(texture); + int red = (color >> 16) & 255; + int green = (color >> 8) & 255; + int blue = color & 255; + if (first != null) { + write(first, sprite, buffer, pose, light, red, green, blue, 255, shade); + } + if (second != null) { + write(second, sprite, buffer, pose, light, red, green, blue, 255, shade); + } + } + + private static void write(Mesh mesh, @Nullable TextureAtlasSprite forced, VertexConsumer buffer, PoseStack.Pose pose, int light, int alpha) { + write(mesh, forced, buffer, pose, light, 255, 255, 255, alpha, false); + } + + private static void write(Mesh mesh, @Nullable TextureAtlasSprite forced, VertexConsumer buffer, PoseStack.Pose pose, int light, int red, int green, int blue, int alpha, boolean shade) { for (Mesh.Quad quad : mesh.quads()) { - TextureAtlasSprite sprite = get(quad.texture); + TextureAtlasSprite sprite = forced == null ? get(quad.texture) : forced; + float face = shade ? faceShade(quad.nx, quad.ny, quad.nz) : 1.0F; + int shadedRed = (int) (red * face); + int shadedGreen = (int) (green * face); + int shadedBlue = (int) (blue * face); for (int vertex = 0; vertex < 4; vertex++) { - put(buffer, pose, quad.x[vertex], quad.y[vertex], quad.z[vertex], sampleU(sprite, quad.u[vertex]), sampleV(sprite, quad.v[vertex]), light, 255, 255, 255, alpha, quad.nx, quad.ny, quad.nz); + put(buffer, pose, quad.x[vertex], quad.y[vertex], quad.z[vertex], sampleU(sprite, quad.u[vertex]), sampleV(sprite, quad.v[vertex]), light, shadedRed, shadedGreen, shadedBlue, alpha, quad.nx, quad.ny, quad.nz); } } } + /** Block-item brightness for the nearest axis. Up is full, down is half, north and south are 0.8, east and west are 0.6. */ + private static float faceShade(float nx, float ny, float nz) { + float ax = Math.abs(nx); + float ay = Math.abs(ny); + float az = Math.abs(nz); + if (ay >= ax && ay >= az) { + return ny > 0 ? 1.0F : 0.5F; + } + if (az >= ax) { + return 0.8F; + } + return 0.6F; + } + public static void drawFluid(Mesh mesh, VertexConsumer buffer, PoseStack.Pose pose, int light, TextureAtlasSprite sprite, int color) { int red = (color >> 16) & 255; int green = (color >> 8) & 255; diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/TemplateScreen.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/TemplateScreen.java index 6f0d620..a7f9ea3 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/client/TemplateScreen.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/TemplateScreen.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import com.mojang.blaze3d.platform.InputConstants; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.screens.Screen; @@ -167,18 +168,28 @@ private void paint(GuiGraphicsExtractor graphics) { graphics.text(this.font, Component.translatable("tooltip.primalpottery.capacity", info.volume()), this.left + 10, this.top + 135 + this.font.lineHeight, -1); } + this.drawHints(graphics); + } + + private void drawHints(GuiGraphicsExtractor graphics) { + Component left = Component.translatable("gui.primalpottery.template.left"); + Component right = Component.translatable("gui.primalpottery.template.right"); + int y = this.top + IMAGE_HEIGHT + 4; + graphics.text(this.font, left, this.left + (IMAGE_WIDTH - this.font.width(left)) / 2, y, 0xFFE0E0E0); + graphics.text(this.font, right, this.left + (IMAGE_WIDTH - this.font.width(right)) / 2, y + this.font.lineHeight, 0xFFE0E0E0); } private boolean sculpt(double mouseX, double mouseY, int button) { int x = (int) Math.floor(mouseX - this.left - PROFILE_X); int y = (int) Math.floor(mouseY - this.top - PROFILE_Y); - if (x < 0 || x >= PROFILE_WIDTH || y < 0 || y >= PROFILE_HEIGHT || this.discs.isEmpty() || (button != 0 && button != 1)) { + if (x < 0 || x >= PROFILE_WIDTH || y < 0 || y >= PROFILE_HEIGHT || this.discs.isEmpty() + || (button != InputConstants.MOUSE_BUTTON_LEFT && button != InputConstants.MOUSE_BUTTON_RIGHT)) { return false; } double value = x * Pottery.MAX_RADIUS / (PROFILE_WIDTH - 1); int row = y * this.discs.size() / PROFILE_HEIGHT; PotteryDisc disc = this.discs.get(row); - if (button == 0) { + if (button == InputConstants.MOUSE_BUTTON_LEFT) { this.discs.set(row, new PotteryDisc(disc.height(), value, Math.min(value, disc.innerRadius()))); } else { this.discs.set(row, new PotteryDisc(disc.height(), disc.radius(), Math.min(disc.radius(), value))); diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/client/WheelRenderer.java b/src/main/java/net/modcrafters/minecraft/primalpottery/client/WheelRenderer.java index b3b08d9..7dbe84c 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/client/WheelRenderer.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/client/WheelRenderer.java @@ -2,14 +2,11 @@ import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Axis; -import java.util.ArrayList; -import java.util.List; import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; //? if >=26.1 import net.minecraft.client.renderer.blockentity.state.BlockEntityRenderState; import net.modcrafters.minecraft.primalpottery.block.PotteryWheelBlockEntity; import net.modcrafters.minecraft.primalpottery.pottery.Pottery; -import net.modcrafters.minecraft.primalpottery.pottery.PotteryDisc; //? if >=26.1 { public final class WheelRenderer implements BlockEntityRenderer { @@ -38,6 +35,7 @@ public void extractRenderState( state.hasClay = blockEntity.hasClay(); state.progress = blockEntity.clayProgress(); state.template = blockEntity.template(); + state.start = blockEntity.start(); } @Override @@ -60,11 +58,11 @@ public void submit( poseStack.translate(16, 0, 16); poseStack.rotate(Axis.YP, -state.angle); poseStack.translate(-16, 0, -16); - Mesh clay = state.hasClay ? clay(Pottery.formed(state.template), state.progress) : null; + Mesh clay = state.hasClay ? clay(Pottery.shaped(state.start, Pottery.formed(state.template), state.progress)) : null; collector.submitCustomGeometry(poseStack, renderType, (pose, buffer) -> { Sprites.draw(SPINNING, buffer, pose, state.lightCoords); if (clay != null) { - Sprites.draw(clay, buffer, pose, state.lightCoords); + Sprites.draw(clay, buffer, pose, state.lightCoords, Sprites.rawTexture()); } }); poseStack.popPose(); @@ -80,6 +78,7 @@ public static final class State extends BlockEntityRenderState { public boolean hasClay; public float progress; public Pottery template; + public Pottery start; } //?} else { /*@Override @@ -106,7 +105,8 @@ public void render( pose = poseStack.last(); Sprites.draw(SPINNING, buffer, pose, light); if (blockEntity.hasClay()) { - Sprites.draw(clay(blockEntity.formed(), blockEntity.clayProgress()), buffer, pose, light); + Pottery shaped = Pottery.shaped(blockEntity.start(), blockEntity.formed(), blockEntity.clayProgress()); + Sprites.draw(clay(shaped), buffer, pose, light, Sprites.rawTexture()); } poseStack.popPose(); poseStack.popPose(); @@ -144,20 +144,13 @@ private static Mesh spinning() { private static Mesh pattern(Pottery pottery) { Mesh mesh = new Mesh(); - mesh.board(30, 24, 2, 2, pottery.discs(), Mesh.Texture.PLANKS); - mesh.fitSprite(); + mesh.edgeBoards(30, 24, 2, pottery.discs(), Mesh.Texture.PLANKS); return mesh; } - private static Mesh clay(Pottery pottery, float progress) { - List formed = new ArrayList<>(); - for (PotteryDisc disc : pottery.discs()) { - double radius = disc.radius() + (4.0 - disc.radius()) * (1.0 - progress); - double inner = disc.innerRadius() * progress; - formed.add(new PotteryDisc(disc.height(), radius, inner)); - } + private static Mesh clay(Pottery pottery) { Mesh mesh = new Mesh(); - mesh.vessel(16, 28, 16, formed, Mesh.Texture.CLAY); + mesh.vessel(16, 28, 16, pottery.discs(), Mesh.Texture.CLAY); mesh.fitSprite(); return mesh; } diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/item/ModItems.java b/src/main/java/net/modcrafters/minecraft/primalpottery/item/ModItems.java index bd063ad..d9a7b17 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/item/ModItems.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/item/ModItems.java @@ -14,11 +14,13 @@ public final class ModItems { public static final ResourceKey WHEEL_ID = itemId("pottery_wheel"); public static final ResourceKey POTTERY_ID = itemId("pottery"); + public static final ResourceKey RAW_POTTERY_ID = itemId("raw_pottery"); public static final ResourceKey BLUEPRINT_ID = itemId("pottery_template"); public static final ResourceKey RAW_TEMPLATE_ID = itemId("raw_pottery_template"); public static final Item WHEEL = new BlockItem(ModBlocks.WHEEL, properties(WHEEL_ID)); public static final Item POTTERY = new PotteryItem(properties(POTTERY_ID).stacksTo(1)); + public static final Item RAW_POTTERY = new RawPotteryItem(properties(RAW_POTTERY_ID).stacksTo(1)); public static final Item BLUEPRINT = new TemplateItem(properties(BLUEPRINT_ID)); public static final Item RAW_TEMPLATE = new TemplateItem(properties(RAW_TEMPLATE_ID)); @@ -28,12 +30,13 @@ private ModItems() { public static void register(BiConsumer out) { out.accept(WHEEL_ID.identifier(), WHEEL); out.accept(POTTERY_ID.identifier(), POTTERY); + out.accept(RAW_POTTERY_ID.identifier(), RAW_POTTERY); out.accept(BLUEPRINT_ID.identifier(), BLUEPRINT); out.accept(RAW_TEMPLATE_ID.identifier(), RAW_TEMPLATE); } public static List tabContents() { - return List.of(new ItemStack(WHEEL), new ItemStack(POTTERY), new ItemStack(RAW_TEMPLATE), new ItemStack(BLUEPRINT)); + return List.of(new ItemStack(WHEEL), new ItemStack(RAW_TEMPLATE)); } public static ItemStack tabIcon() { diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/item/PotteryItem.java b/src/main/java/net/modcrafters/minecraft/primalpottery/item/PotteryItem.java index 1cf3342..fd4707b 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/item/PotteryItem.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/item/PotteryItem.java @@ -1,5 +1,6 @@ package net.modcrafters.minecraft.primalpottery.item; +import java.util.Map; import java.util.function.Consumer; import net.minecraft.core.BlockPos; import net.minecraft.core.component.DataComponents; @@ -41,10 +42,64 @@ //import net.minecraft.world.InteractionResultHolder; public class PotteryItem extends BlockItem { + /** Brighter than the vanilla dye swatches, so a painted piece reads as that colour. */ + private static final Map DYE_COLORS = Map.ofEntries( + Map.entry("white", 0xFFFFFF), + Map.entry("orange", 0xFF5500), + Map.entry("magenta", 0xFF2BD6), + Map.entry("light_blue", 0x2ECFFF), + Map.entry("yellow", 0xFFEA00), + Map.entry("lime", 0x7DFF1A), + Map.entry("pink", 0xFF5C9D), + Map.entry("gray", 0x5A5A5A), + Map.entry("light_gray", 0xC6C6C6), + Map.entry("cyan", 0x00E5E5), + Map.entry("purple", 0xC43BFF), + Map.entry("blue", 0x2F4BFF), + Map.entry("brown", 0xC46A28), + Map.entry("green", 0x3DCC12), + Map.entry("red", 0xFF2A2A), + Map.entry("black", 0x1A1A1A) + ); + public PotteryItem(Properties properties) { super(ModBlocks.POTTERY, properties); } + /** The paint colour, or null when the name is not one of the 16 dyes. */ + public static @Nullable Integer dyeColor(@Nullable String dye) { + return dye == null ? null : DYE_COLORS.get(dye); + } + + /** Points the inventory model at the tinted pottery shape. An unknown dye leaves the stack unchanged. */ + public static void showColor(ItemStack stack, @Nullable String dye) { + Integer color = dyeColor(dye); + if (color == null) { + return; + } + //? if >=26.1 { + stack.set(DataComponents.CUSTOM_MODEL_DATA, new net.minecraft.world.item.component.CustomModelData( + java.util.List.of(), java.util.List.of(), java.util.List.of(), java.util.List.of(color) + )); + //?} else { + /*stack.set(DataComponents.CUSTOM_MODEL_DATA, new net.minecraft.world.item.component.CustomModelData(1)); + *///?} + } + + /** Undyed names stay unstyled. A known dye name uses the shared color map. */ + public static Component dyedName(Component name, @Nullable String dye) { + Integer color = dyeColor(dye); + if (color == null) { + return name; + } + return name.copy().withStyle(style -> style.withColor(color)); + } + + @Override + public Component getName(ItemStack stack) { + return dyedName(read(stack).classify().type().shapeName(), dye(stack)); + } + public static ItemStack stack(Pottery pottery) { return stack(pottery, VesselContents.EMPTY); } @@ -54,6 +109,10 @@ public static ItemStack stack(Pottery pottery, VesselContents contents) { } public static ItemStack stack(Pottery pottery, VesselContents contents, boolean lid) { + return stack(pottery, contents, lid, null); + } + + public static ItemStack stack(Pottery pottery, VesselContents contents, boolean lid, @Nullable String dye) { ItemStack stack = new ItemStack(ModItems.POTTERY); CompoundTag tag = new CompoundTag(); tag.put("pottery", pottery.save()); @@ -61,10 +120,39 @@ public static ItemStack stack(Pottery pottery, VesselContents contents, boolean if (lid) { tag.putBoolean("lid", true); } + if (dye != null && !dye.isEmpty()) { + tag.putString("dye", dye); + } stack.set(DataComponents.CUSTOM_DATA, CustomData.of(tag)); + showColor(stack, dye); return stack; } + public static @Nullable String dye(ItemStack stack) { + CompoundTag tag = customData(stack); + if (tag == null) { + return null; + } + String dye = Nbt.getString(tag, "dye"); + return dye.isEmpty() ? null : dye; + } + + /** Copies the pottery compound onto {@code into} when the stack has one. */ + public static void copyPottery(ItemStack stack, CompoundTag into) { + CompoundTag data = customData(stack); + if (data == null) { + return; + } + CompoundTag pottery = Nbt.compound(data, "pottery"); + if (!pottery.isEmpty()) { + into.put("pottery", pottery.copy()); + } + } + + public static @Nullable CompoundTag customData(ItemStack stack) { + return tag(stack); + } + public static boolean lid(ItemStack stack) { CompoundTag tag = tag(stack); return tag != null && Nbt.getBoolean(tag, "lid"); @@ -224,6 +312,7 @@ public InteractionResult place(BlockPlaceContext context) { Pottery shaped = read(stack); VesselContents contents = contents(stack); boolean closed = lid(stack); + String color = dye(stack); InteractionResult result = super.place(context); if (!result.consumesAction()) { return result; @@ -231,7 +320,7 @@ public InteractionResult place(BlockPlaceContext context) { Level level = context.getLevel(); BlockEntity blockEntity = level.getBlockEntity(context.getClickedPos()); if (blockEntity instanceof PotteryBlockEntity pottery) { - pottery.put(PartHits.quadrant(context.getClickedPos(), context.getClickLocation()), shaped, contents, closed); + pottery.put(PartHits.quadrant(context.getClickedPos(), context.getClickLocation()), shaped, contents, closed, color); } return result; } diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/item/RawPotteryItem.java b/src/main/java/net/modcrafters/minecraft/primalpottery/item/RawPotteryItem.java new file mode 100644 index 0000000..75cea8e --- /dev/null +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/item/RawPotteryItem.java @@ -0,0 +1,111 @@ +package net.modcrafters.minecraft.primalpottery.item; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import net.minecraft.ChatFormatting; +import net.minecraft.core.component.DataComponents; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.network.chat.Component; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.component.CustomData; +//? if >=26.1 +import net.minecraft.world.item.component.TooltipDisplay; +import net.modcrafters.minecraft.primalpottery.pottery.Nbt; +import net.modcrafters.minecraft.primalpottery.pottery.Pottery; +import net.modcrafters.minecraft.primalpottery.pottery.PotteryDisc; +import org.jspecify.annotations.Nullable; + +/** Unfired clay. It is not a block, and it carries no fluid and no lid. */ +public class RawPotteryItem extends Item { + public RawPotteryItem(Properties properties) { + super(properties); + } + + public static ItemStack stack(Pottery pottery) { + return stack(pottery, null); + } + + public static ItemStack stack(Pottery pottery, @Nullable String dye) { + ItemStack stack = new ItemStack(ModItems.RAW_POTTERY); + CompoundTag tag = new CompoundTag(); + tag.put("pottery", pottery.save()); + if (dye != null && !dye.isEmpty()) { + tag.putString("dye", dye); + } + stack.set(DataComponents.CUSTOM_DATA, CustomData.of(tag)); + PotteryItem.showColor(stack, dye); + return stack; + } + + /** + * The profile a wheel may reshape. Null when the stack is dyed, or the pottery compound is missing or invalid. + * {@link Pottery#load} would substitute the display vase for a bad tag, so that stack must not be accepted. + */ + public static @Nullable Pottery wheelProfile(ItemStack stack) { + if (!stack.is(ModItems.RAW_POTTERY) || PotteryItem.dye(stack) != null) { + return null; + } + CompoundTag data = PotteryItem.customData(stack); + if (data == null) { + return null; + } + CompoundTag pottery = Nbt.compound(data, "pottery"); + if (pottery.isEmpty()) { + return null; + } + ListTag list = Nbt.list(pottery, "discs"); + if (list.size() > Pottery.MAX_DISCS) { + return null; + } + List discs = new ArrayList<>(); + for (int index = 0; index < list.size(); index++) { + CompoundTag discTag = Nbt.compoundAt(list, index); + double radius = Nbt.getDouble(discTag, "radius"); + double inner = Nbt.getDouble(discTag, "inner"); + if (inner > radius && inner <= Pottery.MAX_RADIUS) { + inner = radius; + } + discs.add(new PotteryDisc(Nbt.getDouble(discTag, "height"), radius, inner)); + } + if (!Pottery.validProfile(discs)) { + return null; + } + return new Pottery(discs); + } + + @Override + public Component getName(ItemStack stack) { + return PotteryItem.dyedName( + Component.translatable("item.primalpottery.raw_shape", PotteryItem.read(stack).classify().type().shapeName()), + PotteryItem.dye(stack) + ); + } + + //? if >=26.1 { + @Override + public void appendHoverText(ItemStack stack, TooltipContext context, TooltipDisplay display, Consumer tooltip, TooltipFlag flag) { + tooltip(stack, tooltip); + } + //?} else { + /*@Override + public void appendHoverText(ItemStack stack, TooltipContext context, List tooltip, TooltipFlag flag) { + tooltip(stack, tooltip::add); + } + *///?} + + /** Firing, dyeing, and the volume the fired vessel will hold. The colored name stays the title. */ + public static void tooltip(ItemStack stack, Consumer tooltip) { + tooltip.accept(Component.translatable("tooltip.primalpottery.needs_firing").withStyle(ChatFormatting.GRAY)); + if (PotteryItem.dye(stack) == null) { + tooltip.accept(Component.translatable("tooltip.primalpottery.can_be_dyed").withStyle(ChatFormatting.GRAY)); + } + Pottery.Info info = PotteryItem.read(stack).classify(); + if (info.type().isVessel()) { + tooltip.accept(Component.translatable("tooltip.primalpottery.will_contain", info.volume()).withStyle(ChatFormatting.DARK_GRAY)); + } + } +} diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/item/TemplateItem.java b/src/main/java/net/modcrafters/minecraft/primalpottery/item/TemplateItem.java index 6b48c79..7ee6924 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/item/TemplateItem.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/item/TemplateItem.java @@ -24,7 +24,7 @@ //? if <26.1 //import net.minecraft.world.InteractionResultHolder; -/** A reusable profile. Using it sculpts; the table keeps that profile. */ +/** A reusable profile. Using it sculpts. The wheel holds one finished blueprint. */ public final class TemplateItem extends Item { public TemplateItem(Properties properties) { super(properties); diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/network/TemplateSync.java b/src/main/java/net/modcrafters/minecraft/primalpottery/network/TemplateSync.java index f332aec..ddb792c 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/network/TemplateSync.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/network/TemplateSync.java @@ -32,9 +32,19 @@ public static void apply(Player player, boolean mainHand, List disc InteractionHand hand = mainHand ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND; ItemStack stack = player.getItemInHand(hand); if (stack.is(ModItems.RAW_TEMPLATE)) { - ItemStack blueprint = stack.transmuteCopy(ModItems.BLUEPRINT, stack.getCount()); + ItemStack blueprint = new ItemStack(ModItems.BLUEPRINT); TemplateItem.write(blueprint, new Pottery(discs)); - player.setItemInHand(hand, blueprint); + if (stack.getCount() == 1) { + player.setItemInHand(hand, blueprint); + } else { + stack.shrink(1); + if (!player.getInventory().add(blueprint)) { + //? if >=26.1 { + player.drop(blueprint, false, net.minecraft.util.Prediction.SERVER_ONLY); + //?} else + //player.drop(blueprint, false); + } + } } else if (stack.is(ModItems.BLUEPRINT)) { TemplateItem.write(stack, new Pottery(discs)); } diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/Placement.java b/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/Placement.java index e8f580c..e776e4e 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/Placement.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/Placement.java @@ -10,6 +10,7 @@ import net.minecraft.world.phys.Vec3; import net.modcrafters.minecraft.primalpottery.block.ModBlocks; import net.modcrafters.minecraft.primalpottery.block.PotteryBlockEntity; +import net.modcrafters.minecraft.primalpottery.block.PotteryWheelBlock; import net.modcrafters.minecraft.primalpottery.item.ModItems; import org.jspecify.annotations.Nullable; @@ -18,11 +19,15 @@ private Placement() { } public static @Nullable AABB outline(Level level, @Nullable Player player, BlockHitResult hit) { + BlockPos pos = hit.getBlockPos(); + var state = level.getBlockState(pos); + if (state.is(ModBlocks.WHEEL)) { + return PotteryWheelBlock.outline(state, pos); + } if (player == null || !player.getMainHandItem().is(ModItems.POTTERY)) { return null; } - BlockPos pos = hit.getBlockPos(); - if (level.getBlockState(pos).is(ModBlocks.POTTERY)) { + if (state.is(ModBlocks.POTTERY)) { if (!(level.getBlockEntity(pos) instanceof PotteryBlockEntity pottery)) { return null; } diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/Pottery.java b/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/Pottery.java index 5dccdd4..d1f8b28 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/Pottery.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/Pottery.java @@ -9,6 +9,8 @@ public final class Pottery { public static final int MAX_DISCS = 64; public static final double MAX_RADIUS = 5.5; + /** Radius of the clay blank a wheel shapes from when no profile is put back on. */ + public static final double BLANK_RADIUS = 4; private static final double MAX_HEIGHT = 12; // Height of the wheel's editable clay blank. // The clay mesh uses regular octagons whose vertices lie on the disc radius. private static final double CAVITY_AREA_FACTOR = 2 * Math.sqrt(2); @@ -197,6 +199,67 @@ public static Pottery formed(Pottery template) { return template == null ? wheelShape() : template; } + /** + * The only lerp from a blank or a returned profile toward {@code target}. + * Progress at least 1 returns {@code target}. Below 0 is 0. + * The result keeps the target's disc count and heights. + */ + public static Pottery shaped(@Nullable Pottery start, Pottery target, double progress) { + if (progress >= 1) { + return target; + } + if (progress < 0) { + progress = 0; + } + double targetHeight = height(target); + double startHeight = start == null ? 0 : height(start); + List formed = new ArrayList<>(); + double above = 0; + for (PotteryDisc disc : target.discs) { + double sampleRadius = BLANK_RADIUS; + double sampleInner = 0; + if (start != null) { + double fraction = targetHeight == 0 ? 0 : (above + disc.height() / 2.0) / targetHeight; + PotteryDisc sample = sample(start, fraction * startHeight, startHeight); + sampleRadius = sample.radius(); + sampleInner = sample.innerRadius(); + } + double radius = sampleRadius + (disc.radius() - sampleRadius) * progress; + double inner = sampleInner + (disc.innerRadius() - sampleInner) * progress; + radius = Math.min(MAX_RADIUS, Math.max(0, radius)); + inner = Math.min(radius, Math.max(0, inner)); + formed.add(new PotteryDisc(disc.height(), radius, inner)); + above += disc.height(); + } + return new Pottery(formed); + } + + private static double height(Pottery pottery) { + double height = 0; + for (PotteryDisc disc : pottery.discs) { + height += disc.height(); + } + return height; + } + + /** Walks from the top. A point on a disc's end is the next disc. The total height keeps the last disc. */ + private static PotteryDisc sample(Pottery start, double point, double total) { + List discs = start.discs; + if (point >= total) { + return discs.get(discs.size() - 1); + } + double walked = 0; + for (int index = 0; index < discs.size(); index++) { + PotteryDisc disc = discs.get(index); + double end = walked + disc.height(); + if (point < end || index == discs.size() - 1) { + return disc; + } + walked = end; + } + return discs.get(discs.size() - 1); + } + public static Pottery wheelShape() { return new Pottery(List.of( new PotteryDisc(1, 2), diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/PotteryType.java b/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/PotteryType.java index 0255943..89b554b 100644 --- a/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/PotteryType.java +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/pottery/PotteryType.java @@ -8,8 +8,8 @@ public enum PotteryType { LUMP("Just a lump of clay", ChatFormatting.DARK_RED), LUMP_NO_BOTTOM("A pipe?", ChatFormatting.DARK_RED), LUMP_THIN("Too thin!", ChatFormatting.DARK_RED), - POT("A pot!", ChatFormatting.DARK_AQUA), - JAR("A jar!", ChatFormatting.DARK_AQUA), + POT("A bowl!", ChatFormatting.DARK_AQUA), + JAR("Pottery", ChatFormatting.DARK_AQUA), JUG("A jug!", ChatFormatting.DARK_AQUA); private final String label; @@ -36,4 +36,14 @@ public Component tooltipName() { PotteryType type = this.isVessel() ? this : LUMP; return Component.translatable("pottery.primalpottery.type." + type.name().toLowerCase(Locale.ROOT)); } + + /** Wide mouth is a bowl, narrow mouth is a jug, and every other shape is pottery. */ + public Component shapeName() { + String shape = switch (this) { + case JUG -> "jug"; + case POT -> "bowl"; + default -> "pottery"; + }; + return Component.translatable("pottery.primalpottery.shape." + shape); + } } diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/recipe/FirePotteryRecipe.java b/src/main/java/net/modcrafters/minecraft/primalpottery/recipe/FirePotteryRecipe.java new file mode 100644 index 0000000..c5163dc --- /dev/null +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/recipe/FirePotteryRecipe.java @@ -0,0 +1,136 @@ +package net.modcrafters.minecraft.primalpottery.recipe; + +import com.mojang.serialization.MapCodec; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.CustomData; +import net.minecraft.world.item.crafting.AbstractCookingRecipe; +import net.minecraft.world.item.crafting.Ingredient; +//? if >=26.1 +import net.minecraft.world.item.crafting.Recipe; +import net.minecraft.world.item.crafting.RecipeSerializer; +import net.minecraft.world.item.crafting.RecipeType; +import net.minecraft.world.level.Level; +import net.minecraft.core.component.DataComponents; +import net.modcrafters.minecraft.primalpottery.item.ModItems; +import net.modcrafters.minecraft.primalpottery.item.PotteryItem; +//? if >=1.21 +import net.minecraft.world.item.crafting.SingleRecipeInput; + +/** Smelts raw pottery, dyed or not, into the fired item. The parent result is never used. */ +public final class FirePotteryRecipe extends AbstractCookingRecipe { + public static final FirePotteryRecipe INSTANCE = new FirePotteryRecipe(); + //? if >=26.1 { + public static final RecipeSerializer SERIALIZER = new RecipeSerializer<>(MapCodec.unit(INSTANCE), StreamCodec.unit(INSTANCE)); + //?} else { + /*public static final RecipeSerializer SERIALIZER = new RecipeSerializer<>() { + @Override + public MapCodec codec() { + return MapCodec.unit(INSTANCE); + } + + @Override + public StreamCodec streamCodec() { + return StreamCodec.unit(INSTANCE); + } + }; + *///?} + + //? if >=26.1 { + private FirePotteryRecipe() { + super( + new Recipe.CommonInfo(false), + new CookingBookInfo(net.minecraft.world.item.crafting.CookingBookCategory.MISC, ""), + Ingredient.of(ModItems.RAW_POTTERY), + new net.minecraft.world.item.ItemStackTemplate(ModItems.POTTERY), + 0.1F, + 200 + ); + } + //?} else { + /*private FirePotteryRecipe() { + super( + RecipeType.SMELTING, + "", + net.minecraft.world.item.crafting.CookingBookCategory.MISC, + Ingredient.of(ModItems.RAW_POTTERY), + new ItemStack(ModItems.POTTERY), + 0.1F, + 200 + ); + } + *///?} + + //? if >=26.1 { + @Override + public boolean matches(SingleRecipeInput input, Level level) { + return input.item().is(ModItems.RAW_POTTERY); + } + + @Override + public ItemStack assemble(SingleRecipeInput input) { + return this.fired(input.item()); + } + //?} elif >=1.21 { + /*@Override + public boolean matches(SingleRecipeInput input, Level level) { + return input.item().is(ModItems.RAW_POTTERY); + } + + @Override + public ItemStack assemble(SingleRecipeInput input, net.minecraft.core.HolderLookup.Provider registries) { + return this.fired(input.item()); + } + *///?} else { + /*@Override + public boolean matches(net.minecraft.world.Container input, Level level) { + return input.getItem(0).is(ModItems.RAW_POTTERY); + } + + @Override + public ItemStack assemble(net.minecraft.world.Container input, net.minecraft.core.HolderLookup.Provider registries) { + return this.fired(input.getItem(0)); + } + *///?} + + @Override + public boolean isSpecial() { + return true; + } + + @Override + public RecipeSerializer getSerializer() { + return SERIALIZER; + } + + @Override + public RecipeType getType() { + return RecipeType.SMELTING; + } + + //? if >=26.1 { + @Override + protected net.minecraft.world.item.Item furnaceIcon() { + return net.minecraft.world.item.Items.FURNACE; + } + + @Override + public net.minecraft.world.item.crafting.RecipeBookCategory recipeBookCategory() { + return net.minecraft.world.item.crafting.RecipeBookCategories.FURNACE_MISC; + } + //?} + + private ItemStack fired(ItemStack raw) { + ItemStack result = new ItemStack(ModItems.POTTERY); + CompoundTag tag = new CompoundTag(); + PotteryItem.copyPottery(raw, tag); + String dye = PotteryItem.dye(raw); + if (dye != null) { + tag.putString("dye", dye); + } + result.set(DataComponents.CUSTOM_DATA, CustomData.of(tag)); + PotteryItem.showColor(result, dye); + return result; + } +} diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/recipe/ModRecipes.java b/src/main/java/net/modcrafters/minecraft/primalpottery/recipe/ModRecipes.java new file mode 100644 index 0000000..f33bf86 --- /dev/null +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/recipe/ModRecipes.java @@ -0,0 +1,16 @@ +package net.modcrafters.minecraft.primalpottery.recipe; + +import java.util.function.BiConsumer; +import net.minecraft.resources.Identifier; +import net.minecraft.world.item.crafting.RecipeSerializer; +import net.modcrafters.minecraft.primalpottery.Ids; + +public final class ModRecipes { + private ModRecipes() { + } + + public static void register(BiConsumer> out) { + out.accept(Ids.of("paint_pottery"), PaintPotteryRecipe.SERIALIZER); + out.accept(Ids.of("fire_pottery"), FirePotteryRecipe.SERIALIZER); + } +} diff --git a/src/main/java/net/modcrafters/minecraft/primalpottery/recipe/PaintPotteryRecipe.java b/src/main/java/net/modcrafters/minecraft/primalpottery/recipe/PaintPotteryRecipe.java new file mode 100644 index 0000000..6548d98 --- /dev/null +++ b/src/main/java/net/modcrafters/minecraft/primalpottery/recipe/PaintPotteryRecipe.java @@ -0,0 +1,151 @@ +package net.modcrafters.minecraft.primalpottery.recipe; + +import com.mojang.serialization.MapCodec; +import java.util.function.IntFunction; +import net.minecraft.core.component.DataComponents; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.world.item.DyeColor; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.CustomData; +import net.minecraft.world.item.crafting.CustomRecipe; +import net.minecraft.world.item.crafting.RecipeSerializer; +import net.minecraft.world.level.Level; +import net.modcrafters.minecraft.primalpottery.item.ModItems; +import net.modcrafters.minecraft.primalpottery.item.PotteryItem; +import org.jspecify.annotations.Nullable; +//? if >=1.21 +import net.minecraft.world.item.crafting.CraftingInput; + +/** One undyed raw pot and one vanilla dye. The grid size is not read here. */ +public final class PaintPotteryRecipe extends CustomRecipe { + public static final PaintPotteryRecipe INSTANCE = new PaintPotteryRecipe(); + //? if >=26.1 { + public static final RecipeSerializer SERIALIZER = new RecipeSerializer<>(MapCodec.unit(INSTANCE), StreamCodec.unit(INSTANCE)); + //?} else { + /*public static final RecipeSerializer SERIALIZER = new RecipeSerializer<>() { + @Override + public MapCodec codec() { + return MapCodec.unit(INSTANCE); + } + + @Override + public StreamCodec streamCodec() { + return StreamCodec.unit(INSTANCE); + } + }; + *///?} + + private PaintPotteryRecipe() { + //? if <26.1 + //super(net.minecraft.world.item.crafting.CraftingBookCategory.MISC); + } + + //? if >=26.1 { + @Override + public boolean matches(CraftingInput input, Level level) { + return this.matches(input.size(), input::getItem); + } + + @Override + public ItemStack assemble(CraftingInput input) { + return this.assemble(input.size(), input::getItem); + } + //?} elif >=1.21 { + /*@Override + public boolean matches(CraftingInput input, Level level) { + return this.matches(input.size(), input::getItem); + } + + @Override + public ItemStack assemble(CraftingInput input, net.minecraft.core.HolderLookup.Provider registries) { + return this.assemble(input.size(), input::getItem); + } + *///?} else { + /*@Override + public boolean matches(net.minecraft.world.inventory.CraftingContainer input, Level level) { + return this.matches(input.getContainerSize(), input::getItem); + } + + @Override + public ItemStack assemble(net.minecraft.world.inventory.CraftingContainer input, net.minecraft.core.HolderLookup.Provider registries) { + return this.assemble(input.getContainerSize(), input::getItem); + } + *///?} + + @Override + public RecipeSerializer getSerializer() { + return SERIALIZER; + } + + //? if <26.1 { + /*@Override + public boolean canCraftInDimensions(int width, int height) { + return (width == 2 && height == 2) || (width == 3 && height == 3); + } + *///?} + + private boolean matches(int size, IntFunction slot) { + return this.found(size, slot) != null; + } + + private ItemStack assemble(int size, IntFunction slot) { + Found found = this.found(size, slot); + if (found == null) { + return ItemStack.EMPTY; + } + ItemStack result = new ItemStack(ModItems.RAW_POTTERY); + CompoundTag tag = new CompoundTag(); + PotteryItem.copyPottery(found.raw, tag); + tag.putString("dye", found.color.getName()); + result.set(DataComponents.CUSTOM_DATA, CustomData.of(tag)); + PotteryItem.showColor(result, found.color.getName()); + return result; + } + + private @Nullable Found found(int size, IntFunction slot) { + ItemStack raw = ItemStack.EMPTY; + DyeColor color = null; + for (int index = 0; index < size; index++) { + ItemStack stack = slot.apply(index); + if (stack.isEmpty()) { + continue; + } + if (raw.isEmpty() && stack.is(ModItems.RAW_POTTERY) && PotteryItem.dye(stack) == null) { + raw = stack; + continue; + } + DyeColor dye = vanillaDye(stack); + if (color == null && dye != null) { + color = dye; + continue; + } + return null; + } + if (raw.isEmpty() || color == null) { + return null; + } + return new Found(raw, color); + } + + private static @Nullable DyeColor vanillaDye(ItemStack stack) { + //? if >=26.1 { + DyeColor color = stack.get(DataComponents.DYE); + if (color == null || !stack.is(net.minecraft.world.item.Items.DYE.pick(color))) { + return null; + } + return color; + //?} else { + /*if (!(stack.getItem() instanceof net.minecraft.world.item.DyeItem dye)) { + return null; + } + if (!stack.is(net.minecraft.world.item.DyeItem.byColor(dye.getDyeColor()))) { + return null; + } + return dye.getDyeColor(); + *///?} + } + + private record Found(ItemStack raw, DyeColor color) { + } +} diff --git a/src/main/resources/assets/primalpottery/items/pottery.json b/src/main/resources/assets/primalpottery/items/pottery.json index edfa6ca..3c171a2 100644 --- a/src/main/resources/assets/primalpottery/items/pottery.json +++ b/src/main/resources/assets/primalpottery/items/pottery.json @@ -1,6 +1,26 @@ { "model": { - "type": "minecraft:model", - "model": "primalpottery:block/pottery" + "type": "minecraft:select", + "property": "minecraft:display_context", + "cases": [ + { + "when": "on_shelf", + "model": { + "type": "minecraft:special", + "base": "primalpottery:block/pottery", + "model": { + "type": "primalpottery:pottery", + "on_shelf": true + } + } + } + ], + "fallback": { + "type": "minecraft:special", + "base": "primalpottery:block/pottery", + "model": { + "type": "primalpottery:pottery" + } + } } } diff --git a/src/main/resources/assets/primalpottery/items/raw_pottery.json b/src/main/resources/assets/primalpottery/items/raw_pottery.json new file mode 100644 index 0000000..956e85a --- /dev/null +++ b/src/main/resources/assets/primalpottery/items/raw_pottery.json @@ -0,0 +1,26 @@ +{ + "model": { + "type": "minecraft:select", + "property": "minecraft:display_context", + "cases": [ + { + "when": "on_shelf", + "model": { + "type": "minecraft:special", + "base": "primalpottery:item/raw_pottery", + "model": { + "type": "primalpottery:pottery", + "on_shelf": true + } + } + } + ], + "fallback": { + "type": "minecraft:special", + "base": "primalpottery:item/raw_pottery", + "model": { + "type": "primalpottery:pottery" + } + } + } +} diff --git a/src/main/resources/assets/primalpottery/lang/en_us.json b/src/main/resources/assets/primalpottery/lang/en_us.json index 9e27df3..546b052 100644 --- a/src/main/resources/assets/primalpottery/lang/en_us.json +++ b/src/main/resources/assets/primalpottery/lang/en_us.json @@ -4,13 +4,23 @@ "block.primalpottery.pottery": "Pottery", "item.primalpottery.pottery_wheel": "Pottery Wheel", "item.primalpottery.pottery": "Pottery", + "item.primalpottery.raw_pottery": "Raw Pottery", + "item.primalpottery.raw_shape": "Raw %s", "item.primalpottery.pottery_template": "Pottery Blueprint", "item.primalpottery.raw_pottery_template": "Raw Pottery Template", "tooltip.primalpottery.type": "Type: %s", + "tooltip.primalpottery.needs_firing": "Needs firing", + "tooltip.primalpottery.can_be_dyed": "Can be dyed", + "tooltip.primalpottery.will_contain": "Will contain %s mL", "tooltip.primalpottery.capacity": "Capacity: %s mL", + "gui.primalpottery.template.left": "left-click to shape the outside", + "gui.primalpottery.template.right": "right-click to create the inside", "tooltip.primalpottery.contents": "%s: %s mL", "pottery.primalpottery.type.lump": "Lump", - "pottery.primalpottery.type.pot": "Pot", - "pottery.primalpottery.type.jar": "Jar", - "pottery.primalpottery.type.jug": "Jug" + "pottery.primalpottery.type.pot": "Bowl", + "pottery.primalpottery.type.jar": "Pottery", + "pottery.primalpottery.type.jug": "Jug", + "pottery.primalpottery.shape.jug": "Jug", + "pottery.primalpottery.shape.bowl": "Bowl", + "pottery.primalpottery.shape.pottery": "Pottery" } diff --git a/src/main/resources/assets/primalpottery/models/block/pottery.json b/src/main/resources/assets/primalpottery/models/block/pottery.json index e8dd7e9..f8cfd48 100644 --- a/src/main/resources/assets/primalpottery/models/block/pottery.json +++ b/src/main/resources/assets/primalpottery/models/block/pottery.json @@ -5,10 +5,10 @@ "clay": "minecraft:block/red_terracotta" }, "elements": [ - { "from": [6, 0, 6], "to": [10, 1, 10], "faces": { "down": { "texture": "#clay" }, "up": { "texture": "#clay" }, "north": { "texture": "#clay" }, "south": { "texture": "#clay" }, "west": { "texture": "#clay" }, "east": { "texture": "#clay" } } }, - { "from": [4.5, 1, 4.5], "to": [11.5, 7, 11.5], "faces": { "down": { "texture": "#clay" }, "up": { "texture": "#clay" }, "north": { "texture": "#clay" }, "south": { "texture": "#clay" }, "west": { "texture": "#clay" }, "east": { "texture": "#clay" } } }, - { "from": [5.5, 7, 5.5], "to": [10.5, 9, 10.5], "faces": { "down": { "texture": "#clay" }, "up": { "texture": "#clay" }, "north": { "texture": "#clay" }, "south": { "texture": "#clay" }, "west": { "texture": "#clay" }, "east": { "texture": "#clay" } } }, - { "from": [6.5, 9, 6.5], "to": [9.5, 11, 9.5], "faces": { "down": { "texture": "#clay" }, "up": { "texture": "#clay" }, "north": { "texture": "#clay" }, "south": { "texture": "#clay" }, "west": { "texture": "#clay" }, "east": { "texture": "#clay" } } }, - { "from": [5.5, 11, 5.5], "to": [10.5, 12.5, 10.5], "faces": { "down": { "texture": "#clay" }, "up": { "texture": "#clay" }, "north": { "texture": "#clay" }, "south": { "texture": "#clay" }, "west": { "texture": "#clay" }, "east": { "texture": "#clay" } } } + { "from": [6, 0, 6], "to": [10, 1, 10], "faces": { "down": { "texture": "#clay", "tintindex": 0 }, "up": { "texture": "#clay", "tintindex": 0 }, "north": { "texture": "#clay", "tintindex": 0 }, "south": { "texture": "#clay", "tintindex": 0 }, "west": { "texture": "#clay", "tintindex": 0 }, "east": { "texture": "#clay", "tintindex": 0 } } }, + { "from": [4.5, 1, 4.5], "to": [11.5, 7, 11.5], "faces": { "down": { "texture": "#clay", "tintindex": 0 }, "up": { "texture": "#clay", "tintindex": 0 }, "north": { "texture": "#clay", "tintindex": 0 }, "south": { "texture": "#clay", "tintindex": 0 }, "west": { "texture": "#clay", "tintindex": 0 }, "east": { "texture": "#clay", "tintindex": 0 } } }, + { "from": [5.5, 7, 5.5], "to": [10.5, 9, 10.5], "faces": { "down": { "texture": "#clay", "tintindex": 0 }, "up": { "texture": "#clay", "tintindex": 0 }, "north": { "texture": "#clay", "tintindex": 0 }, "south": { "texture": "#clay", "tintindex": 0 }, "west": { "texture": "#clay", "tintindex": 0 }, "east": { "texture": "#clay", "tintindex": 0 } } }, + { "from": [6.5, 9, 6.5], "to": [9.5, 11, 9.5], "faces": { "down": { "texture": "#clay", "tintindex": 0 }, "up": { "texture": "#clay", "tintindex": 0 }, "north": { "texture": "#clay", "tintindex": 0 }, "south": { "texture": "#clay", "tintindex": 0 }, "west": { "texture": "#clay", "tintindex": 0 }, "east": { "texture": "#clay", "tintindex": 0 } } }, + { "from": [5.5, 11, 5.5], "to": [10.5, 12.5, 10.5], "faces": { "down": { "texture": "#clay", "tintindex": 0 }, "up": { "texture": "#clay", "tintindex": 0 }, "north": { "texture": "#clay", "tintindex": 0 }, "south": { "texture": "#clay", "tintindex": 0 }, "west": { "texture": "#clay", "tintindex": 0 }, "east": { "texture": "#clay", "tintindex": 0 } } } ] } diff --git a/src/main/resources/assets/primalpottery/models/item/pottery_dyed.json b/src/main/resources/assets/primalpottery/models/item/pottery_dyed.json new file mode 100644 index 0000000..b1bb8a4 --- /dev/null +++ b/src/main/resources/assets/primalpottery/models/item/pottery_dyed.json @@ -0,0 +1,7 @@ +{ + "parent": "primalpottery:block/pottery", + "textures": { + "particle": "minecraft:block/white_concrete", + "clay": "minecraft:block/white_concrete" + } +} diff --git a/src/main/resources/assets/primalpottery/models/item/raw_pottery.json b/src/main/resources/assets/primalpottery/models/item/raw_pottery.json new file mode 100644 index 0000000..9d0563d --- /dev/null +++ b/src/main/resources/assets/primalpottery/models/item/raw_pottery.json @@ -0,0 +1,7 @@ +{ + "parent": "primalpottery:block/pottery", + "textures": { + "particle": "minecraft:block/clay", + "clay": "minecraft:block/clay" + } +} diff --git a/src/test/java/net/modcrafters/minecraft/primalpottery/client/MeshTest.java b/src/test/java/net/modcrafters/minecraft/primalpottery/client/MeshTest.java index 5258ea3..36eddb7 100644 --- a/src/test/java/net/modcrafters/minecraft/primalpottery/client/MeshTest.java +++ b/src/test/java/net/modcrafters/minecraft/primalpottery/client/MeshTest.java @@ -46,6 +46,35 @@ void potWallsFaceOutward() { } } + @Test + void layersShareTheWallShade() { + Mesh mesh = new Mesh(); + mesh.vessel(0, 0, 0, List.of(new PotteryDisc(2, 6, 2), new PotteryDisc(2, 3, 1)), Mesh.Texture.CLAY); + int walls = 0; + int steps = 0; + for (Mesh.Quad quad : mesh.quads()) { + boolean flat = quad.y[0] == quad.y[1] && quad.y[1] == quad.y[2] && quad.y[2] == quad.y[3]; + if (flat) { + if (quad.y[0] == 0 || quad.y[0] == 4) { + continue; + } + steps++; + assertEquals(0, quad.ny, 0.001F); + float mx = (quad.x[0] + quad.x[1] + quad.x[2] + quad.x[3]) / 4.0F; + float mz = (quad.z[0] + quad.z[1] + quad.z[2] + quad.z[3]) / 4.0F; + float dot = mx * quad.nx + mz * quad.nz; + assertTrue(Math.hypot(mx, mz) > 2.5 ? dot > 0 : dot < 0); + continue; + } + walls++; + assertEquals(0, quad.ny, 0.001F); + float length = (float) Math.sqrt(quad.nx * quad.nx + quad.nz * quad.nz); + assertEquals(1, length, 0.001F); + } + assertEquals(32, walls); + assertEquals(16, steps); + } + @Test void zeroWidthWallsDoNotProduceFaces() { Mesh mesh = new Mesh(); @@ -101,6 +130,29 @@ void spinningPreviewStaysUprightInsideTheRecess() { } } + @Test + void templateStandsOnEveryEdge() { + Mesh mesh = new Mesh(); + mesh.edgeBoards(30, 24, 2, List.of(new PotteryDisc(2, 4), new PotteryDisc(2, 6)), Mesh.Texture.PLANKS); + assertTrue(reaches(mesh, 29, true, true)); + assertTrue(reaches(mesh, 29, false, true)); + assertTrue(reaches(mesh, 3, true, false)); + assertTrue(reaches(mesh, 3, false, false)); + for (Mesh.Quad quad : mesh.quads()) { + float minX = quad.x[0]; + float maxZ = quad.z[0]; + float minZ = quad.z[0]; + for (int vertex = 1; vertex < 4; vertex++) { + minX = Math.min(minX, quad.x[vertex]); + maxZ = Math.max(maxZ, quad.z[vertex]); + minZ = Math.min(minZ, quad.z[vertex]); + } + if (minX > 20) { + assertTrue(minZ >= 14 && maxZ <= 18); + } + } + } + @Test void boardPushesIntoTheNarrowerClay() { Mesh mesh = new Mesh(); @@ -135,6 +187,18 @@ private static float reach(Mesh mesh, float low, float high) { return reach; } + private static boolean reaches(Mesh mesh, float limit, boolean alongX, boolean high) { + for (Mesh.Quad quad : mesh.quads()) { + for (int vertex = 0; vertex < 4; vertex++) { + float value = alongX ? quad.x[vertex] : quad.z[vertex]; + if (high ? value > limit : value < limit) { + return true; + } + } + } + return false; + } + private static float westEdge(Mesh mesh, float low, float high) { float edge = Float.POSITIVE_INFINITY; for (Mesh.Quad quad : mesh.quads()) { diff --git a/src/test/java/net/modcrafters/minecraft/primalpottery/pottery/PotteryTest.java b/src/test/java/net/modcrafters/minecraft/primalpottery/pottery/PotteryTest.java index 1af9829..67d9fbe 100644 --- a/src/test/java/net/modcrafters/minecraft/primalpottery/pottery/PotteryTest.java +++ b/src/test/java/net/modcrafters/minecraft/primalpottery/pottery/PotteryTest.java @@ -3,28 +3,134 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import net.minecraft.ChatFormatting; import net.minecraft.SharedConstants; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; import net.minecraft.server.Bootstrap; +import net.minecraft.world.item.DyeColor; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.BubbleColumnBlock; import net.minecraft.world.level.block.LiquidBlock; import net.minecraft.world.level.block.SlabBlock; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluids; +import net.minecraft.core.Registry; +import net.modcrafters.minecraft.primalpottery.Ids; +import net.modcrafters.minecraft.primalpottery.block.ModBlocks; +import net.modcrafters.minecraft.primalpottery.client.Sprites; +import net.modcrafters.minecraft.primalpottery.item.ModItems; import net.modcrafters.minecraft.primalpottery.item.PotteryItem; +import net.modcrafters.minecraft.primalpottery.item.RawPotteryItem; +import net.modcrafters.minecraft.primalpottery.recipe.FirePotteryRecipe; +import net.modcrafters.minecraft.primalpottery.recipe.PaintPotteryRecipe; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; class PotteryTest { + private static final Map DYE_COLORS = Map.ofEntries( + Map.entry("white", 0xFFFFFF), + Map.entry("orange", 0xFF5500), + Map.entry("magenta", 0xFF2BD6), + Map.entry("light_blue", 0x2ECFFF), + Map.entry("yellow", 0xFFEA00), + Map.entry("lime", 0x7DFF1A), + Map.entry("pink", 0xFF5C9D), + Map.entry("gray", 0x5A5A5A), + Map.entry("light_gray", 0xC6C6C6), + Map.entry("cyan", 0x00E5E5), + Map.entry("purple", 0xC43BFF), + Map.entry("blue", 0x2F4BFF), + Map.entry("brown", 0xC46A28), + Map.entry("green", 0x3DCC12), + Map.entry("red", 0xFF2A2A), + Map.entry("black", 0x1A1A1A) + ); + @BeforeAll static void bootstrap() { SharedConstants.tryDetectVersion(); Bootstrap.bootStrap(); + // Freezing the registries drops the intrusive-holder map. ModItems still constructs its + // blocks through that map, so put it back before the first touch. + openIntrusiveHolders(); + } + + private static boolean componentsBound(net.minecraft.world.item.Item item) { + try { + item.components(); + return true; + } catch (NullPointerException unbound) { + return false; + } + } + + private static void openIntrusiveHolders() { + try { + Class type = Class.forName("net.minecraft.core.MappedRegistry"); + java.lang.reflect.Field holders = null; + java.lang.reflect.Field frozen = null; + for (java.lang.reflect.Field field : type.getDeclaredFields()) { + if (holders == null && java.util.Map.class.isAssignableFrom(field.getType()) && field.getName().toLowerCase(java.util.Locale.ROOT).contains("intrusive")) { + holders = field; + } + if (frozen == null && field.getType() == boolean.class && field.getName().toLowerCase(java.util.Locale.ROOT).contains("frozen")) { + frozen = field; + } + } + if (holders == null) { + throw new IllegalStateException("no intrusive holder field"); + } + holders.setAccessible(true); + if (frozen != null) { + frozen.setAccessible(true); + } + for (Object registry : List.of(BuiltInRegistries.BLOCK, BuiltInRegistries.ITEM)) { + if (frozen != null) { + frozen.set(registry, false); + } + if (holders.get(registry) == null) { + holders.set(registry, new java.util.IdentityHashMap<>()); + } + } + Class.forName("net.modcrafters.minecraft.primalpottery.item.ModItems"); + Registry.register(BuiltInRegistries.BLOCK, ModBlocks.WHEEL_ID, ModBlocks.WHEEL); + Registry.register(BuiltInRegistries.BLOCK, ModBlocks.POTTERY_ID, ModBlocks.POTTERY); + Registry.register(BuiltInRegistries.ITEM, ModItems.WHEEL_ID, ModItems.WHEEL); + Registry.register(BuiltInRegistries.ITEM, ModItems.POTTERY_ID, ModItems.POTTERY); + Registry.register(BuiltInRegistries.ITEM, ModItems.RAW_POTTERY_ID, ModItems.RAW_POTTERY); + Registry.register(BuiltInRegistries.ITEM, ModItems.BLUEPRINT_ID, ModItems.BLUEPRINT); + Registry.register(BuiltInRegistries.ITEM, ModItems.RAW_TEMPLATE_ID, ModItems.RAW_TEMPLATE); + java.lang.reflect.Method bind = null; + java.lang.reflect.Method holderOf = null; + Object empty = null; + for (net.minecraft.world.item.Item item : BuiltInRegistries.ITEM) { + if (componentsBound(item)) { + continue; + } + if (bind == null) { + empty = net.minecraft.core.component.DataComponentMap.class.getField("EMPTY").get(null); + bind = net.minecraft.core.Holder.Reference.class.getMethod( + "bindComponents", net.minecraft.core.component.DataComponentMap.class); + holderOf = net.minecraft.world.item.Item.class.getMethod("builtInRegistryHolder"); + } + bind.invoke(holderOf.invoke(item), empty); + } + for (Object registry : List.of(BuiltInRegistries.BLOCK, BuiltInRegistries.ITEM)) { + frozen.set(registry, true); + } + } catch (ReflectiveOperationException | ExceptionInInitializerError failure) { + throw new IllegalStateException(failure); + } } @Test @@ -74,6 +180,187 @@ void wheelWithoutATemplateKeepsTheVase() { assertEquals(2, Pottery.formed(null).discs().getFirst().radius(), 0.0001); } + @Test + void shapedIsTheOnlyLerp() { + assertEquals(4, Pottery.BLANK_RADIUS, 0); + Pottery target = new Pottery(List.of(new PotteryDisc(1, 3, 1))); + Pottery blank = Pottery.shaped(null, target, 0); + assertEquals(1, blank.discs().getFirst().height(), 0.0001); + assertEquals(4, blank.discs().getFirst().radius(), 0.0001); + assertEquals(0, blank.discs().getFirst().innerRadius(), 0.0001); + Pottery half = Pottery.shaped(null, target, 0.5); + assertEquals(3.5, half.discs().getFirst().radius(), 0.0001); + assertEquals(0.5, half.discs().getFirst().innerRadius(), 0.0001); + assertSame(target, Pottery.shaped(null, target, 1)); + assertSame(target, Pottery.shaped(null, target, 2)); + assertEquals(blank.discs(), Pottery.shaped(null, target, -0.2).discs()); + + Pottery start = new Pottery(List.of(new PotteryDisc(1, 2, 0))); + Pottery wider = new Pottery(List.of(new PotteryDisc(1, 4, 2))); + assertEquals(start.discs(), Pottery.shaped(start, wider, 0).discs()); + Pottery mid = Pottery.shaped(start, wider, 0.5); + assertEquals(3, mid.discs().getFirst().radius(), 0.0001); + assertEquals(1, mid.discs().getFirst().innerRadius(), 0.0001); + + Pottery stacked = new Pottery(List.of(new PotteryDisc(2, 1, 0), new PotteryDisc(2, 5, 0))); + Pottery pair = new Pottery(List.of(new PotteryDisc(2, 4, 2), new PotteryDisc(2, 4, 2))); + Pottery held = Pottery.shaped(stacked, pair, 0); + assertEquals(1, held.discs().get(0).radius(), 0.0001); + assertEquals(5, held.discs().get(1).radius(), 0.0001); + + Pottery edge = new Pottery(List.of(new PotteryDisc(1, 1), new PotteryDisc(1, 5))); + Pottery tall = new Pottery(List.of(new PotteryDisc(2, 4))); + assertEquals(5, Pottery.shaped(edge, tall, 0).discs().getFirst().radius(), 0.0001); + + Pottery vase = Pottery.shaped(null, Pottery.formed(null), 0.5); + assertEquals(3, vase.discs().getFirst().radius(), 0.0001); + assertEquals(Pottery.formed(null).discs().size(), vase.discs().size()); + assertTrue(Pottery.validProfile(vase.discs())); + assertTrue(Pottery.validProfile(Pottery.shaped(stacked, pair, 0.5).discs())); + assertEquals(Pottery.formed(null).discs(), Pottery.shaped(null, Pottery.formed(null), 1).discs()); + } + + @Test + void itemNameUsesJugBowlOrPottery() { + assertEquals(Component.translatable("pottery.primalpottery.shape.bowl"), vessel(5).classify().type().shapeName()); + assertEquals(Component.translatable("pottery.primalpottery.shape.pottery"), vessel(4).classify().type().shapeName()); + assertEquals(Component.translatable("pottery.primalpottery.shape.jug"), vessel(2.7).classify().type().shapeName()); + assertEquals( + Component.translatable("item.primalpottery.raw_shape", Component.translatable("pottery.primalpottery.shape.jug")), + RawPotteryItem.stack(vessel(2.7)).getHoverName() + ); + assertEquals( + Component.translatable("pottery.primalpottery.shape.bowl"), + PotteryItem.stack(vessel(5)).getHoverName() + ); + } + + @Test + void rawTooltipSaysItNeedsFiringAndHowMuchItWillHold() { + Pottery jar = new Pottery(List.of( + new PotteryDisc(1, 4, 3), + new PotteryDisc(2, 4, 2), + new PotteryDisc(1, 4, 0) + )); + List plain = new ArrayList<>(); + RawPotteryItem.tooltip(RawPotteryItem.stack(jar), plain::add); + assertEquals(List.of( + Component.translatable("tooltip.primalpottery.needs_firing").withStyle(ChatFormatting.GRAY), + Component.translatable("tooltip.primalpottery.can_be_dyed").withStyle(ChatFormatting.GRAY), + Component.translatable("tooltip.primalpottery.will_contain", jar.classify().volume()).withStyle(ChatFormatting.DARK_GRAY) + ), plain); + + List dyed = new ArrayList<>(); + RawPotteryItem.tooltip(RawPotteryItem.stack(jar, "red"), dyed::add); + assertEquals(List.of( + Component.translatable("tooltip.primalpottery.needs_firing").withStyle(ChatFormatting.GRAY), + Component.translatable("tooltip.primalpottery.will_contain", jar.classify().volume()).withStyle(ChatFormatting.DARK_GRAY) + ), dyed); + + List lump = new ArrayList<>(); + RawPotteryItem.tooltip(RawPotteryItem.stack(new Pottery(List.of(new PotteryDisc(1, 2, 0)))), lump::add); + assertEquals(List.of( + Component.translatable("tooltip.primalpottery.needs_firing").withStyle(ChatFormatting.GRAY), + Component.translatable("tooltip.primalpottery.can_be_dyed").withStyle(ChatFormatting.GRAY) + ), lump); + } + + @Test + void dyeRoundTripsAndClayTextureUsesTerracotta() { + Pottery profile = new Pottery(List.of(new PotteryDisc(1, 2, 0))); + ItemStack raw = RawPotteryItem.stack(profile, DyeColor.BLUE.getName()); + assertFalse(ModItems.RAW_POTTERY instanceof net.minecraft.world.item.BlockItem); + assertEquals(1, new ItemStack(ModItems.RAW_POTTERY).getMaxStackSize()); + CompoundTag tag = PotteryItem.customData(raw); + assertEquals("blue", Nbt.getString(tag, "dye")); + assertEquals(profile.discs(), Pottery.load(Nbt.compound(tag, "pottery")).discs()); + assertFalse(tag.contains("fluid")); + assertFalse(tag.contains("lid")); + assertEquals(0x2F4BFF, raw.getItem().getName(raw).getStyle().getColor().getValue()); + assertEquals(16, DyeColor.values().length); + for (DyeColor color : DyeColor.values()) { + ItemStack tinted = RawPotteryItem.stack(profile, color.getName()); + assertEquals(DYE_COLORS.get(color.getName()).intValue(), tinted.getItem().getName(tinted).getStyle().getColor().getValue(), color.getName()); + } + ItemStack plain = RawPotteryItem.stack(profile); + assertNull(plain.getItem().getName(plain).getStyle().getColor()); + assertNull(RawPotteryItem.wheelProfile(raw)); + assertEquals(profile.discs(), RawPotteryItem.wheelProfile(plain).discs()); + + ItemStack missing = new ItemStack(ModItems.RAW_POTTERY); + assertNull(RawPotteryItem.wheelProfile(missing)); + CompoundTag broken = new CompoundTag(); + broken.put("pottery", new Pottery(List.of(new PotteryDisc(0, 2))).save()); + missing.set(net.minecraft.core.component.DataComponents.CUSTOM_DATA, net.minecraft.world.item.component.CustomData.of(broken)); + assertNull(RawPotteryItem.wheelProfile(missing)); + assertEquals(Pottery.display().discs(), Pottery.load(Nbt.compound(broken, "pottery")).discs()); + + ItemStack fired = PotteryItem.stack(profile, VesselContents.EMPTY, false, "blue"); + ItemStack undyed = PotteryItem.stack(profile); + assertEquals(PotteryItem.read(undyed).classify(), PotteryItem.read(fired).classify()); + assertEquals("blue", PotteryItem.dye(fired)); + assertNull(PotteryItem.dye(undyed)); + assertEquals(0x2F4BFF, fired.getItem().getName(fired).getStyle().getColor().getValue()); + assertNull(undyed.getItem().getName(undyed).getStyle().getColor()); + assertEquals(Ids.of("minecraft", "block/white_concrete"), Sprites.clayTexture("blue")); + assertEquals(0x2F4BFF, Sprites.dyeTint("blue")); + assertEquals(0xFFFFFF, Sprites.dyeTint(null)); + assertEquals(Ids.of("minecraft", "block/clay"), Sprites.rawTexture()); + assertEquals(Ids.of("minecraft", "block/red_terracotta"), Sprites.clayTexture(null)); + assertEquals(Ids.of("minecraft", "block/red_terracotta"), Sprites.clayTexture("blue_dye")); + } + + @Test + void paintAndFireKeepTheProfile() { + Pottery profile = new Pottery(List.of(new PotteryDisc(1, 2, 0), new PotteryDisc(1, 3, 1))); + ItemStack raw = RawPotteryItem.stack(profile); + ItemStack dye = vanillaDye(DyeColor.BLUE); + assertTrue(paintMatches(2, 2, pad(4, raw, dye))); + assertTrue(paintMatches(3, 3, pad(9, raw, dye))); + assertTrue(paintMatches(2, 1, pad(2, raw, dye))); + assertFalse(paintMatches(3, 1, pad(3, raw, dye, new ItemStack(Items.STICK)))); + assertFalse(paintMatches(2, 1, pad(2, RawPotteryItem.stack(profile, "blue"), dye))); + assertFalse(paintMatches(2, 1, pad(2, raw, outsideDye()))); + ItemStack painted = paintAssemble(2, 1, pad(2, raw, dye)); + assertEquals(profile.discs(), PotteryItem.read(painted).discs()); + assertEquals("blue", PotteryItem.dye(painted)); + for (DyeColor color : DyeColor.values()) { + ItemStack one = paintAssemble(2, 1, pad(2, RawPotteryItem.stack(profile), vanillaDye(color))); + assertEquals(color.getName(), PotteryItem.dye(one), color.getName()); + assertEquals(profile.discs(), PotteryItem.read(one).discs(), color.getName()); + assertTrue(paintMatches(2, 2, pad(4, RawPotteryItem.stack(profile), vanillaDye(color))), color.getName()); + } + assertEquals(PaintPotteryRecipe.SERIALIZER, PaintPotteryRecipe.INSTANCE.getSerializer()); + //? if <26.1 { + /*assertTrue(PaintPotteryRecipe.INSTANCE.canCraftInDimensions(2, 2)); + assertTrue(PaintPotteryRecipe.INSTANCE.canCraftInDimensions(3, 3)); + assertFalse(PaintPotteryRecipe.INSTANCE.canCraftInDimensions(2, 1)); + *///?} + + assertTrue(fireMatches(raw)); + assertTrue(fireMatches(painted)); + assertFalse(fireMatches(PotteryItem.stack(profile))); + ItemStack fired = fireAssemble(painted); + assertTrue(fired.is(ModItems.POTTERY)); + assertEquals(profile.discs(), PotteryItem.read(fired).discs()); + assertTrue(PotteryItem.contents(fired).isEmpty()); + assertFalse(PotteryItem.lid(fired)); + assertEquals("blue", PotteryItem.dye(fired)); + ItemStack clear = fireAssemble(raw); + assertNull(PotteryItem.dye(clear)); + assertEquals(profile.discs(), PotteryItem.read(clear).discs()); + assertEquals(FirePotteryRecipe.SERIALIZER, FirePotteryRecipe.INSTANCE.getSerializer()); + //? if >=26.1 { + assertEquals(net.minecraft.world.item.crafting.RecipeType.SMELTING, FirePotteryRecipe.INSTANCE.getType()); + assertEquals(net.minecraft.world.item.crafting.RecipeBookCategories.FURNACE_MISC, FirePotteryRecipe.INSTANCE.recipeBookCategory()); + assertEquals(200, FirePotteryRecipe.INSTANCE.cookingTime()); + assertEquals(0.1F, FirePotteryRecipe.INSTANCE.experience(), 0.0001F); + //?} else { + /*assertEquals(200, FirePotteryRecipe.INSTANCE.getCookingTime()); + assertEquals(0.1F, FirePotteryRecipe.INSTANCE.getExperience(), 0.0001F); + *///?} + } + @Test void wheelFormsTheInstalledTemplate() { Pottery template = new Pottery(List.of(new PotteryDisc(1, 3, 1))); @@ -321,6 +608,82 @@ private static BlockState flowing(net.minecraft.world.level.block.Block block) { return block.defaultBlockState().setValue(LiquidBlock.LEVEL, 1); } + private static List pad(int size, ItemStack... stacks) { + java.util.ArrayList items = new java.util.ArrayList<>(); + for (ItemStack stack : stacks) { + items.add(stack); + } + while (items.size() < size) { + items.add(ItemStack.EMPTY); + } + return items; + } + + private static ItemStack vanillaDye(DyeColor color) { + //? if >=26.1 { + ItemStack stack = new ItemStack(Items.DYE.pick(color)); + // Unit tests never run the server component bake, so a dye item can have an empty prototype. + if (stack.get(net.minecraft.core.component.DataComponents.DYE) == null) { + stack.set(net.minecraft.core.component.DataComponents.DYE, color); + } + return stack; + //?} else + //return new ItemStack(net.minecraft.world.item.DyeItem.byColor(color)); + } + + private static ItemStack outsideDye() { + ItemStack stick = new ItemStack(Items.STICK); + //? if >=26.1 + stick.set(net.minecraft.core.component.DataComponents.DYE, DyeColor.BLUE); + return stick; + } + + private static boolean paintMatches(int width, int height, List items) { + //? if >=1.21 { + return PaintPotteryRecipe.INSTANCE.matches(net.minecraft.world.item.crafting.CraftingInput.of(width, height, items), null); + //?} else { + /*return PaintPotteryRecipe.INSTANCE.matches(crafting(width, height, items), null); + *///?} + } + + private static ItemStack paintAssemble(int width, int height, List items) { + //? if >=26.1 { + return PaintPotteryRecipe.INSTANCE.assemble(net.minecraft.world.item.crafting.CraftingInput.of(width, height, items)); + //?} elif >=1.21 { + /*return PaintPotteryRecipe.INSTANCE.assemble(net.minecraft.world.item.crafting.CraftingInput.of(width, height, items), null); + *///?} else { + /*return PaintPotteryRecipe.INSTANCE.assemble(crafting(width, height, items), null); + *///?} + } + + private static boolean fireMatches(ItemStack stack) { + //? if >=1.21 { + return FirePotteryRecipe.INSTANCE.matches(new net.minecraft.world.item.crafting.SingleRecipeInput(stack), null); + //?} else { + /*return FirePotteryRecipe.INSTANCE.matches(new net.minecraft.world.SimpleContainer(stack), null); + *///?} + } + + private static ItemStack fireAssemble(ItemStack stack) { + //? if >=26.1 { + return FirePotteryRecipe.INSTANCE.assemble(new net.minecraft.world.item.crafting.SingleRecipeInput(stack)); + //?} elif >=1.21 { + /*return FirePotteryRecipe.INSTANCE.assemble(new net.minecraft.world.item.crafting.SingleRecipeInput(stack), null); + *///?} else { + /*return FirePotteryRecipe.INSTANCE.assemble(new net.minecraft.world.SimpleContainer(stack), null); + *///?} + } + + //? if <1.21 { + /*private static net.minecraft.world.inventory.CraftingContainer crafting(int width, int height, List items) { + net.minecraft.core.NonNullList slots = net.minecraft.core.NonNullList.withSize(width * height, ItemStack.EMPTY); + for (int index = 0; index < items.size() && index < slots.size(); index++) { + slots.set(index, items.get(index)); + } + return new net.minecraft.world.inventory.TransientCraftingContainer(null, width, height, slots); + } + *///?} + private static Pottery vessel(double opening) { return new Pottery(List.of( new PotteryDisc(1, opening + 0.5, opening), diff --git a/versions/1.20.6/src/main/resources/assets/primalpottery/models/item/pottery.json b/versions/1.20.6/src/main/resources/assets/primalpottery/models/item/pottery.json new file mode 100644 index 0000000..75e043d --- /dev/null +++ b/versions/1.20.6/src/main/resources/assets/primalpottery/models/item/pottery.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:builtin/entity", + "textures": { + "particle": "minecraft:block/red_terracotta" + } +} diff --git a/versions/1.20.6/src/main/resources/assets/primalpottery/models/item/raw_pottery.json b/versions/1.20.6/src/main/resources/assets/primalpottery/models/item/raw_pottery.json new file mode 100644 index 0000000..4b0675f --- /dev/null +++ b/versions/1.20.6/src/main/resources/assets/primalpottery/models/item/raw_pottery.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:builtin/entity", + "textures": { + "particle": "minecraft:block/clay" + } +} diff --git a/versions/1.20.6/src/main/resources/data/primalpottery/advancements/recipes/pottery.json b/versions/1.20.6/src/main/resources/data/primalpottery/advancements/recipes/pottery.json index 3e6408c..afddef9 100644 --- a/versions/1.20.6/src/main/resources/data/primalpottery/advancements/recipes/pottery.json +++ b/versions/1.20.6/src/main/resources/data/primalpottery/advancements/recipes/pottery.json @@ -15,7 +15,9 @@ "rewards": { "recipes": [ "primalpottery:pottery_wheel", - "primalpottery:raw_pottery_template" + "primalpottery:raw_pottery_template", + "primalpottery:paint_pottery", + "primalpottery:fire_pottery" ] } } diff --git a/versions/1.20.6/src/main/resources/data/primalpottery/recipes/fire_pottery.json b/versions/1.20.6/src/main/resources/data/primalpottery/recipes/fire_pottery.json new file mode 100644 index 0000000..80f1daa --- /dev/null +++ b/versions/1.20.6/src/main/resources/data/primalpottery/recipes/fire_pottery.json @@ -0,0 +1,3 @@ +{ + "type": "primalpottery:fire_pottery" +} diff --git a/versions/1.20.6/src/main/resources/data/primalpottery/recipes/paint_pottery.json b/versions/1.20.6/src/main/resources/data/primalpottery/recipes/paint_pottery.json new file mode 100644 index 0000000..9054e40 --- /dev/null +++ b/versions/1.20.6/src/main/resources/data/primalpottery/recipes/paint_pottery.json @@ -0,0 +1,3 @@ +{ + "type": "primalpottery:paint_pottery" +} diff --git a/versions/1.21.1/src/main/resources/assets/primalpottery/models/item/pottery.json b/versions/1.21.1/src/main/resources/assets/primalpottery/models/item/pottery.json new file mode 100644 index 0000000..75e043d --- /dev/null +++ b/versions/1.21.1/src/main/resources/assets/primalpottery/models/item/pottery.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:builtin/entity", + "textures": { + "particle": "minecraft:block/red_terracotta" + } +} diff --git a/versions/1.21.1/src/main/resources/assets/primalpottery/models/item/raw_pottery.json b/versions/1.21.1/src/main/resources/assets/primalpottery/models/item/raw_pottery.json new file mode 100644 index 0000000..4b0675f --- /dev/null +++ b/versions/1.21.1/src/main/resources/assets/primalpottery/models/item/raw_pottery.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:builtin/entity", + "textures": { + "particle": "minecraft:block/clay" + } +} diff --git a/versions/1.21.1/src/main/resources/data/primalpottery/advancement/recipes/pottery.json b/versions/1.21.1/src/main/resources/data/primalpottery/advancement/recipes/pottery.json index 3e6408c..afddef9 100644 --- a/versions/1.21.1/src/main/resources/data/primalpottery/advancement/recipes/pottery.json +++ b/versions/1.21.1/src/main/resources/data/primalpottery/advancement/recipes/pottery.json @@ -15,7 +15,9 @@ "rewards": { "recipes": [ "primalpottery:pottery_wheel", - "primalpottery:raw_pottery_template" + "primalpottery:raw_pottery_template", + "primalpottery:paint_pottery", + "primalpottery:fire_pottery" ] } } diff --git a/versions/1.21.1/src/main/resources/data/primalpottery/recipe/fire_pottery.json b/versions/1.21.1/src/main/resources/data/primalpottery/recipe/fire_pottery.json new file mode 100644 index 0000000..80f1daa --- /dev/null +++ b/versions/1.21.1/src/main/resources/data/primalpottery/recipe/fire_pottery.json @@ -0,0 +1,3 @@ +{ + "type": "primalpottery:fire_pottery" +} diff --git a/versions/1.21.1/src/main/resources/data/primalpottery/recipe/paint_pottery.json b/versions/1.21.1/src/main/resources/data/primalpottery/recipe/paint_pottery.json new file mode 100644 index 0000000..9054e40 --- /dev/null +++ b/versions/1.21.1/src/main/resources/data/primalpottery/recipe/paint_pottery.json @@ -0,0 +1,3 @@ +{ + "type": "primalpottery:paint_pottery" +} diff --git a/versions/26.3/src/main/resources/data/primalpottery/advancement/recipes/pottery.json b/versions/26.3/src/main/resources/data/primalpottery/advancement/recipes/pottery.json index 3e6408c..afddef9 100644 --- a/versions/26.3/src/main/resources/data/primalpottery/advancement/recipes/pottery.json +++ b/versions/26.3/src/main/resources/data/primalpottery/advancement/recipes/pottery.json @@ -15,7 +15,9 @@ "rewards": { "recipes": [ "primalpottery:pottery_wheel", - "primalpottery:raw_pottery_template" + "primalpottery:raw_pottery_template", + "primalpottery:paint_pottery", + "primalpottery:fire_pottery" ] } } diff --git a/versions/26.3/src/main/resources/data/primalpottery/recipe/fire_pottery.json b/versions/26.3/src/main/resources/data/primalpottery/recipe/fire_pottery.json new file mode 100644 index 0000000..80f1daa --- /dev/null +++ b/versions/26.3/src/main/resources/data/primalpottery/recipe/fire_pottery.json @@ -0,0 +1,3 @@ +{ + "type": "primalpottery:fire_pottery" +} diff --git a/versions/26.3/src/main/resources/data/primalpottery/recipe/paint_pottery.json b/versions/26.3/src/main/resources/data/primalpottery/recipe/paint_pottery.json new file mode 100644 index 0000000..9054e40 --- /dev/null +++ b/versions/26.3/src/main/resources/data/primalpottery/recipe/paint_pottery.json @@ -0,0 +1,3 @@ +{ + "type": "primalpottery:paint_pottery" +}