From 78800a3c0768c06e46aedf0b422b09172e326c04 Mon Sep 17 00:00:00 2001 From: Vladislav Kuznetsov Date: Sat, 13 Apr 2024 16:19:26 +0300 Subject: [PATCH] Some testing refactoring --- src/main/java/com/youpe/test/Testing.java | 40 ++++-------------- .../java/com/youpe/test/TestingClient.java | 2 + .../com/youpe/test/event/KeyInputHandler.java | 5 ++- .../com/youpe/test/event/TestHudRender.java | 36 ++++++++++++++++ .../java/com/youpe/test/item/ModItems.java | 39 +++++++++++++++++ .../test/{ => item/custom}/CustomItem.java | 14 +++--- .../resources/assets/testing/lang/en_us.json | 4 +- ...tom_item.json => custom_item_example.json} | 2 +- ...ustom_item.png => custom_item_example.png} | Bin src/main/resources/testing.mixins.json | 1 - 10 files changed, 101 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/youpe/test/event/TestHudRender.java create mode 100644 src/main/java/com/youpe/test/item/ModItems.java rename src/main/java/com/youpe/test/{ => item/custom}/CustomItem.java (61%) rename src/main/resources/assets/testing/models/item/{custom_item.json => custom_item_example.json} (51%) rename src/main/resources/assets/testing/textures/item/{custom_item.png => custom_item_example.png} (100%) diff --git a/src/main/java/com/youpe/test/Testing.java b/src/main/java/com/youpe/test/Testing.java index 15fdd87..a0bdfe9 100644 --- a/src/main/java/com/youpe/test/Testing.java +++ b/src/main/java/com/youpe/test/Testing.java @@ -1,5 +1,8 @@ package com.youpe.test; +import com.youpe.test.event.TestHudRender; +import com.youpe.test.item.ModItems; +import com.youpe.test.item.custom.CustomItem; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.fabricmc.fabric.api.item.v1.FabricItemSettings; @@ -24,40 +27,15 @@ import org.slf4j.LoggerFactory; import com.mojang.blaze3d.systems.RenderSystem; public class Testing implements ModInitializer { - // This logger is used to write text to the console and the log file. - // It is considered best practice to use your mod id as the logger's name. - // That way, it's clear which mod wrote info, warnings, and errors. - public static final Logger LOGGER = LoggerFactory.getLogger("testing"); - public static final Item CUSTOM_ITEM = new CustomItem(new FabricItemSettings().rarity(Rarity.COMMON)); + + public static final String MOD_ID = "testing"; + public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + @Override public void onInitialize() { - // This code runs as soon as Minecraft is in a mod-load-ready state. - // However, some things (like resources) may still be uninitialized. - // Proceed with mild caution. - LOGGER.info("Hello Fabric world!"); - Registry.register(Registries.ITEM, new Identifier("testing", "custom_item"), CUSTOM_ITEM); - FuelRegistry.INSTANCE.add(CUSTOM_ITEM, 30000); - ItemGroupEvents.modifyEntriesEvent(ItemGroups.BUILDING_BLOCKS).register(content -> { - content.add(CUSTOM_ITEM); - }); - HudRenderCallback.EVENT.register((drawContext, tickDelta) -> { - Matrix4f positionMatrix = drawContext.getMatrices().peek().getPositionMatrix(); - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder buffer = tessellator.getBuffer(); - - buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE); - buffer.vertex(positionMatrix, 20, 20, 0).color(1f, 1f, 1f, 1f).texture(0f, 0f).next(); - buffer.vertex(positionMatrix, 20, 60, 0).color(1f, 0f, 0f, 1f).texture(0f, 1f).next(); - buffer.vertex(positionMatrix, 60, 60, 0).color(0f, 1f, 0f, 1f).texture(1f, 1f).next(); - buffer.vertex(positionMatrix, 60, 20, 0).color(0f, 0f, 1f, 1f).texture(1f, 0f).next(); - - RenderSystem.setShader(GameRenderer::getPositionColorTexProgram); - RenderSystem.setShaderTexture(0, new Identifier("testing", "icon.png")); - RenderSystem.setShaderColor(1f, 1f, 1f, 1f); - - tessellator.draw(); - }); + ModItems.registerModItems(); + } } \ No newline at end of file diff --git a/src/main/java/com/youpe/test/TestingClient.java b/src/main/java/com/youpe/test/TestingClient.java index 1dffd69..492a002 100644 --- a/src/main/java/com/youpe/test/TestingClient.java +++ b/src/main/java/com/youpe/test/TestingClient.java @@ -2,6 +2,7 @@ package com.youpe.test; import com.youpe.test.event.KeyInputHandler; +import com.youpe.test.event.TestHudRender; import net.fabricmc.api.ClientModInitializer; public class TestingClient implements ClientModInitializer{ @@ -10,6 +11,7 @@ public class TestingClient implements ClientModInitializer{ public void onInitializeClient() { KeyInputHandler.register(); KeyInputHandler.registerKeyInputs(); + TestHudRender.registerModRenders(); } } diff --git a/src/main/java/com/youpe/test/event/KeyInputHandler.java b/src/main/java/com/youpe/test/event/KeyInputHandler.java index f5739c7..a1651b5 100644 --- a/src/main/java/com/youpe/test/event/KeyInputHandler.java +++ b/src/main/java/com/youpe/test/event/KeyInputHandler.java @@ -1,5 +1,6 @@ package com.youpe.test.event; +import com.youpe.test.Testing; import org.lwjgl.glfw.GLFW; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +15,7 @@ import net.minecraft.client.util.InputUtil; import net.minecraft.text.Text; public class KeyInputHandler { - public static final Logger LOGGER = LoggerFactory.getLogger("testing"); + public static final String KEY_CATEGORY_TESTING = "key.category.testing.tutorial"; public static final String KEY_TEST = "key.testing.test"; @@ -23,7 +24,7 @@ public class KeyInputHandler { public static void registerKeyInputs() { ClientTickEvents.END_CLIENT_TICK.register(client -> { if (modkey.wasPressed()) { - LOGGER.info("NAZHAL PIZDEC BLYA"); + Testing.LOGGER.info("NAZHAL PIZDEC BLYA"); client.player.sendMessage(Text.literal("PIZDEC")); MinecraftClient.getInstance().setScreen(new GUI(Text.empty())); } diff --git a/src/main/java/com/youpe/test/event/TestHudRender.java b/src/main/java/com/youpe/test/event/TestHudRender.java new file mode 100644 index 0000000..1913aec --- /dev/null +++ b/src/main/java/com/youpe/test/event/TestHudRender.java @@ -0,0 +1,36 @@ +package com.youpe.test.event; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.youpe.test.Testing; +import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.render.*; +import net.minecraft.util.Identifier; +import org.joml.Matrix4f; + +public class TestHudRender { + + private static void addTestRendering(DrawContext drawContext, float tickDelta){ + Matrix4f positionMatrix = drawContext.getMatrices().peek().getPositionMatrix(); + Tessellator tessellator = Tessellator.getInstance(); + BufferBuilder buffer = tessellator.getBuffer(); + + buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE); + buffer.vertex(positionMatrix, 20, 20, 0).color(1f, 1f, 1f, 1f).texture(0f, 0f).next(); + buffer.vertex(positionMatrix, 20, 60, 0).color(1f, 0f, 0f, 1f).texture(0f, 1f).next(); + buffer.vertex(positionMatrix, 60, 60, 0).color(0f, 1f, 0f, 1f).texture(1f, 1f).next(); + buffer.vertex(positionMatrix, 60, 20, 0).color(0f, 0f, 1f, 1f).texture(1f, 0f).next(); + + RenderSystem.setShader(GameRenderer::getPositionColorTexProgram); + RenderSystem.setShaderTexture(0, new Identifier("testing", "icon.png")); + RenderSystem.setShaderColor(1f, 1f, 1f, 1f); + + tessellator.draw(); + } + + + public static void registerModRenders(){ + Testing.LOGGER.info("Registering ModRenders for " + Testing.MOD_ID); + HudRenderCallback.EVENT.register(TestHudRender::addTestRendering); + } +} diff --git a/src/main/java/com/youpe/test/item/ModItems.java b/src/main/java/com/youpe/test/item/ModItems.java new file mode 100644 index 0000000..e0338e4 --- /dev/null +++ b/src/main/java/com/youpe/test/item/ModItems.java @@ -0,0 +1,39 @@ +package com.youpe.test.item; + +import com.youpe.test.Testing; +import com.youpe.test.item.custom.CustomItem; +import net.fabricmc.fabric.api.item.v1.FabricItemSettings; +import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroupEntries; +import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; +import net.minecraft.item.Item; +import net.minecraft.item.ItemGroups; +import net.minecraft.registry.Registries; +import net.minecraft.registry.Registry; +import net.minecraft.util.Identifier; +import net.minecraft.util.Rarity; + +public class ModItems { + + // Items list + public static final Item CUSTOM_ITEM_EXAMPLE = registerItem("custom_item_example", + new CustomItem(new FabricItemSettings().rarity(Rarity.EPIC))); + + // Adding to specific ItemGroup (using ItemGroupsEvents) + public static void addItemsToBuildingBlocks(FabricItemGroupEntries entries){ + entries.add(CUSTOM_ITEM_EXAMPLE); + } + + // Register item via Registry + private static Item registerItem(String name, Item item){ + return Registry.register(Registries.ITEM, new Identifier(Testing.MOD_ID, name), item); + } + + // Using in class which implementing ModInitializer + public static void registerModItems(){ + Testing.LOGGER.info("Registering Mod Items for " + Testing.MOD_ID); + + ItemGroupEvents.modifyEntriesEvent(ItemGroups.BUILDING_BLOCKS).register(ModItems::addItemsToBuildingBlocks); + + } + +} diff --git a/src/main/java/com/youpe/test/CustomItem.java b/src/main/java/com/youpe/test/item/custom/CustomItem.java similarity index 61% rename from src/main/java/com/youpe/test/CustomItem.java rename to src/main/java/com/youpe/test/item/custom/CustomItem.java index 4bca1b9..d7e4112 100644 --- a/src/main/java/com/youpe/test/CustomItem.java +++ b/src/main/java/com/youpe/test/item/custom/CustomItem.java @@ -1,4 +1,4 @@ -package com.youpe.test; +package com.youpe.test.item.custom; import java.util.List; @@ -6,6 +6,8 @@ import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Formatting; @@ -21,16 +23,18 @@ public class CustomItem extends Item{ @Override public TypedActionResult use(World world, PlayerEntity playerEntity, Hand hand) { - playerEntity.openHandledScreen(null); - playerEntity.playSound(SoundEvents.BLOCK_BELL_USE, 1.0F, 1.0F); + if (world.isClient) {return super.use(world, playerEntity, hand);} + + world.playSound(null, playerEntity.getBlockPos(), SoundEvents.BLOCK_BELL_USE, SoundCategory.PLAYERS, 1.0F, 1.0F); +// playerEntity.playSound(SoundEvents.BLOCK_BELL_USE, 1.0F, 1.0F); return TypedActionResult.success(playerEntity.getStackInHand(hand)); } @Override public void appendTooltip(ItemStack itemStack, World world, List tooltip, TooltipContext tooltipContext) { // default white text - tooltip.add(Text.translatable("item.testing.custom_item.tooltip")); + tooltip.add(Text.translatable("item.testing.custom_item_example.tooltip")); // formatted red text - tooltip.add(Text.translatable("item.testing.custom_item.tooltip").formatted(Formatting.RED)); + tooltip.add(Text.translatable("item.testing.custom_item_example.tooltip").formatted(Formatting.DARK_RED)); } } diff --git a/src/main/resources/assets/testing/lang/en_us.json b/src/main/resources/assets/testing/lang/en_us.json index b0c53ef..6766096 100644 --- a/src/main/resources/assets/testing/lang/en_us.json +++ b/src/main/resources/assets/testing/lang/en_us.json @@ -1,6 +1,6 @@ { - "item.testing.custom_item": "Pizdec", - "item.testing.custom_item.tooltip": "hahaha", + "item.testing.custom_item_example": "Pizdec", + "item.testing.custom_item_example.tooltip": "hahaha", "key.category.testing.tutorial": "Testing", "key.testing.test": "Test" } \ No newline at end of file diff --git a/src/main/resources/assets/testing/models/item/custom_item.json b/src/main/resources/assets/testing/models/item/custom_item_example.json similarity index 51% rename from src/main/resources/assets/testing/models/item/custom_item.json rename to src/main/resources/assets/testing/models/item/custom_item_example.json index 5c820f9..468df1d 100644 --- a/src/main/resources/assets/testing/models/item/custom_item.json +++ b/src/main/resources/assets/testing/models/item/custom_item_example.json @@ -1,6 +1,6 @@ { "parent": "item/generated", "textures": { - "layer0": "testing:item/custom_item" + "layer0": "testing:item/custom_item_example" } } \ No newline at end of file diff --git a/src/main/resources/assets/testing/textures/item/custom_item.png b/src/main/resources/assets/testing/textures/item/custom_item_example.png similarity index 100% rename from src/main/resources/assets/testing/textures/item/custom_item.png rename to src/main/resources/assets/testing/textures/item/custom_item_example.png diff --git a/src/main/resources/testing.mixins.json b/src/main/resources/testing.mixins.json index 95c76e4..349349a 100644 --- a/src/main/resources/testing.mixins.json +++ b/src/main/resources/testing.mixins.json @@ -3,7 +3,6 @@ "package": "com.youpe.test.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ - "ExampleMixin" ], "injectors": { "defaultRequire": 1