Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Refactor reference mod (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
JR1811 authored Oct 14, 2023
1 parent c0bec4f commit f991e7c
Show file tree
Hide file tree
Showing 24 changed files with 361 additions and 177 deletions.
Original file line number Diff line number Diff line change
@@ -1,146 +1,31 @@
package dev.mineblock11.fabric.referencemod;

import com.mojang.blaze3d.systems.RenderSystem;
import dev.mineblock11.fabric.referencemod.block.ModBlockEntities;
import dev.mineblock11.fabric.referencemod.block.ModBlocks;
import dev.mineblock11.fabric.referencemod.event.ModEvents;
import dev.mineblock11.fabric.referencemod.item.ModItems;
import dev.mineblock11.fabric.referencemod.particle.ModParticles;
import dev.mineblock11.fabric.referencemod.sound.ModSounds;
import dev.mineblock11.fabric.referencemod.util.helper.LoggerUtil;
import dev.mineblock11.fabric.referencemod.util.helper.TextUtil;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.render.*;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.math.Vec3d;
import org.apache.commons.lang3.text.WordUtils;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL11;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyMod implements ModInitializer {
public static final String MOD_ID = "referencemod";

public static String generateHumanReadable(Identifier identifier) {
String identifier_path = identifier.getPath();
String lowercase = identifier_path.replace("_", " ");
String capitalized = WordUtils.capitalize(lowercase);
return capitalized;
}
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

@Override
public void onInitialize() {
ModBlocks.initialize();
ModBlockEntities.registerBlockEntityTypes();
ModItems.initialize();
ModSounds.initializeSounds();
ModEvents.registerAllEvents();
ModParticles.initialize();
TextUtil.initializeAllTextFunctions();

PlayerDiedCallback.EVENT.register((player, deathMessage) -> {
MinecraftClient client = MinecraftClient.getInstance();
ChatHud chat = client.inGameHud.getChatHud();

chat.addMessage(Text.literal(player.getEntityName() + "... you're an absolute idiot."));
});

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(MyMod.MOD_ID, "icon.png"));
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);

tessellator.draw();
});

HudRenderCallback.EVENT.register((drawContext, tickDelta) -> {
MatrixStack matrixStack = drawContext.getMatrices();
matrixStack.push();

matrixStack.translate(40, 40, 0);
matrixStack.multiply(RotationAxis.POSITIVE_Z.rotationDegrees((System.currentTimeMillis() % 5000) / 5000f * 360f));
matrixStack.translate(-40, -40, 0);

Matrix4f positionMatrix = matrixStack.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(MyMod.MOD_ID, "icon.png"));
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);

tessellator.draw();
matrixStack.pop();
});

WorldRenderEvents.END.register(context -> {
Camera camera = context.camera();

Vec3d targetPosition = new Vec3d(0, 100, 0);
Vec3d transformedPosition = targetPosition.subtract(camera.getPos());

MatrixStack matrixStack = new MatrixStack();
matrixStack.multiply(RotationAxis.POSITIVE_X.rotationDegrees(camera.getPitch()));
matrixStack.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(camera.getYaw() + 180.0F));
matrixStack.translate(transformedPosition.x, transformedPosition.y, transformedPosition.z);

Matrix4f positionMatrix = matrixStack.peek().getPositionMatrix();
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.getBuffer();

buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE);
buffer.vertex(positionMatrix, 0, 1, 0).color(1f, 1f, 1f, 1f).texture(0f, 0f).next();
buffer.vertex(positionMatrix, 0, 0, 0).color(1f, 0f, 0f, 1f).texture(0f, 1f).next();
buffer.vertex(positionMatrix, 1, 0, 0).color(0f, 1f, 0f, 1f).texture(1f, 1f).next();
buffer.vertex(positionMatrix, 1, 1, 0).color(0f, 0f, 1f, 1f).texture(1f, 0f).next();

RenderSystem.setShader(GameRenderer::getPositionColorTexProgram);
RenderSystem.setShaderTexture(0, new Identifier(MyMod.MOD_ID, "icon.png"));
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderSystem.disableCull();
RenderSystem.depthFunc(GL11.GL_ALWAYS);

tessellator.draw();

RenderSystem.depthFunc(GL11.GL_LEQUAL);
RenderSystem.enableCull();
});

Text translatable = Text.translatable("my_mod.text.hello");
MutableText mutable = Text.translatable("my_mod.text.bye");

String jsonString = Text.Serializer.toJson(mutable);
MutableText result = Text.Serializer.fromJson(jsonString);

result = Text.literal("Hello World!").formatted(Formatting.AQUA, Formatting.BOLD, Formatting.UNDERLINE);

AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
var playerName = player.getDisplayName().copy();
var entityName = entity.getDisplayName().copy();

MinecraftClient client = MinecraftClient.getInstance();
ChatHud chat = client.inGameHud.getChatHud();

chat.addMessage(Text.of("SNAP!!!!"));
chat.addMessage(playerName.append(" attacked ").append(entityName));

return ActionResult.PASS;
});
LoggerUtil.devLogger("All classes have been initialized");
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package dev.mineblock11.fabric.referencemod.client;
package dev.mineblock11.fabric.referencemod;

import dev.mineblock11.fabric.referencemod.ModParticles;
import dev.mineblock11.fabric.referencemod.MyParticle;
import dev.mineblock11.fabric.referencemod.particle.ModParticles;
import dev.mineblock11.fabric.referencemod.particle.custom.MyParticle;
import dev.mineblock11.fabric.referencemod.util.helper.LoggerUtil;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.minecraft.client.particle.EndRodParticle;

@Environment(EnvType.CLIENT)
public class MyModClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ParticleFactoryRegistry.getInstance().register(ModParticles.MY_PARTICLE, MyParticle.Factory::new);

LoggerUtil.devLogger("All client classes have been initialized");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.mineblock11.fabric.referencemod;
package dev.mineblock11.fabric.referencemod.block;

import dev.mineblock11.fabric.referencemod.block.TestBlockEntity;
import dev.mineblock11.fabric.referencemod.MyMod;
import dev.mineblock11.fabric.referencemod.block.entity.TestBlockEntity;
import dev.mineblock11.fabric.referencemod.util.helper.LoggerUtil;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.registry.Registries;
Expand All @@ -11,6 +13,8 @@ public class ModBlockEntities {
public static BlockEntityType<TestBlockEntity> TEST_BLOCK_ENTITY_TYPE;

public static void registerBlockEntityTypes() {
LoggerUtil.devLogger("Initializing BlockEntities");

TEST_BLOCK_ENTITY_TYPE = Registry.register(
Registries.BLOCK_ENTITY_TYPE, new Identifier(MyMod.MOD_ID, "test_block_entity"),
FabricBlockEntityTypeBuilder.create(TestBlockEntity::new, ModBlocks.TEST_BLOCK).build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.mineblock11.fabric.referencemod;
package dev.mineblock11.fabric.referencemod.block;

import dev.mineblock11.fabric.referencemod.block.PrismarineLampBlock;
import dev.mineblock11.fabric.referencemod.block.TestBlock;
import dev.mineblock11.fabric.referencemod.MyMod;
import dev.mineblock11.fabric.referencemod.block.custom.PrismarineLampBlock;
import dev.mineblock11.fabric.referencemod.block.custom.TestBlock;
import dev.mineblock11.fabric.referencemod.util.helper.LoggerUtil;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.PillarBlock;
Expand Down Expand Up @@ -44,6 +46,6 @@ public static <T extends Block> T register(T block, String name, boolean registe
}

public static void initialize() {

LoggerUtil.devLogger("Initializing Blocks");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.mineblock11.fabric.referencemod.block;
package dev.mineblock11.fabric.referencemod.block.custom;

import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.mineblock11.fabric.referencemod.block;
package dev.mineblock11.fabric.referencemod.block.custom;

import dev.mineblock11.fabric.referencemod.block.entity.TestBlockEntity;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.mineblock11.fabric.referencemod.block;
package dev.mineblock11.fabric.referencemod.block.entity;

import dev.mineblock11.fabric.referencemod.ModBlockEntities;
import dev.mineblock11.fabric.referencemod.block.ModBlockEntities;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dev.mineblock11.fabric.referencemod.datagen;

import dev.mineblock11.fabric.referencemod.datagen.provider.*;
import dev.mineblock11.fabric.referencemod.util.helper.LoggerUtil;
import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;

public class MyModDatagen implements DataGeneratorEntrypoint {
@Override
public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
LoggerUtil.devLogger("Initializing Datagen");
final FabricDataGenerator.Pack pack = fabricDataGenerator.createPack();

// ...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dev.mineblock11.fabric.referencemod.datagen;
package dev.mineblock11.fabric.referencemod.datagen.provider;

import dev.mineblock11.fabric.referencemod.ModBlocks;
import dev.mineblock11.fabric.referencemod.block.ModBlocks;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider;

import java.nio.file.Path;
import java.util.Optional;

public class EnglishTranslationProvider extends FabricLanguageProvider {
protected EnglishTranslationProvider(FabricDataOutput dataOutput) {
public EnglishTranslationProvider(FabricDataOutput dataOutput) {
// Specifying the language code is optional in this case!
// 'en_us' is always the default.
super(dataOutput, "en_us");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.mineblock11.fabric.referencemod.datagen;
package dev.mineblock11.fabric.referencemod.datagen.provider;

import dev.mineblock11.fabric.referencemod.ModBlocks;
import dev.mineblock11.fabric.referencemod.ModItems;
import dev.mineblock11.fabric.referencemod.block.ModBlocks;
import dev.mineblock11.fabric.referencemod.item.ModItems;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricBlockLootTableProvider;
import net.minecraft.enchantment.Enchantments;
Expand All @@ -18,7 +18,7 @@
import net.minecraft.predicate.item.ItemPredicate;

public class MyBlockLootTableProvider extends FabricBlockLootTableProvider {
MyBlockLootTableProvider(FabricDataOutput output) {
public MyBlockLootTableProvider(FabricDataOutput output) {
super(output);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.mineblock11.fabric.referencemod.datagen;
package dev.mineblock11.fabric.referencemod.datagen.provider;

import dev.mineblock11.fabric.referencemod.ModBlocks;
import dev.mineblock11.fabric.referencemod.block.ModBlocks;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider;
import net.minecraft.block.Block;
Expand Down
Loading

0 comments on commit f991e7c

Please sign in to comment.