Skip to content

Commit

Permalink
Initial port for 1.21.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny-Hui committed Nov 8, 2024
1 parent 9f72626 commit 691e4ee
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 62 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.7-SNAPSHOT'
id 'fabric-loom' version '1.8-SNAPSHOT'
id 'maven-publish'
}

Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.21.1
yarn_mappings=1.21.1+build.3
loader_version=0.16.2
minecraft_version=1.21.3
yarn_mappings=1.21.3+build.2
loader_version=0.16.7

# Dependencies
fabric_version=0.102.1+1.21.1
fabric_version=0.106.1+1.21.3

# Mod Properties
mod_version = 1.0.3
mod_version = 1.0.4
maven_group = com.lx862
archives_base_name = gcaps
17 changes: 8 additions & 9 deletions src/main/java/com/lx862/mozccaps/Main.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.lx862.mozccaps;

import com.lx862.mozccaps.armor.CapModel;
import com.lx862.mozccaps.network.Networking;
import net.fabricmc.api.ModInitializer;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.Item;
import net.minecraft.item.equipment.ArmorMaterials;
import net.minecraft.item.equipment.EquipmentType;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;

public class Main implements ModInitializer {
public static final Item CAPS = new ArmorItem(RegistryEntry.of(CapModel.ARMOR_MATERIAL), ArmorItem.Type.HELMET, new Item.Settings());
public static final Item CAPS_STRAPPED = new ArmorItem(RegistryEntry.of(CapModel.ARMOR_MATERIAL), ArmorItem.Type.HELMET, new Item.Settings());

public static final RegistryKey<Item> CAPS_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of("mozc_caps", "caps"));
public static final RegistryKey<Item> CAPS_STRAPPED_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of("mozc_caps", "caps_strapped"));
public static final Item CAPS = Registry.register(Registries.ITEM, CAPS_KEY, new ArmorItem(ArmorMaterials.LEATHER, EquipmentType.HELMET, new Item.Settings().registryKey(CAPS_KEY).useItemPrefixedTranslationKey()));
public static final Item CAPS_STRAPPED = Registry.register(Registries.ITEM, CAPS_STRAPPED_KEY, new ArmorItem(ArmorMaterials.LEATHER, EquipmentType.HELMET, new Item.Settings().registryKey(CAPS_STRAPPED_KEY).useItemPrefixedTranslationKey()));
@Override
public void onInitialize() {
Registry.register(Registries.ARMOR_MATERIAL, Identifier.of("mozc_caps", "armor_material"), CapModel.ARMOR_MATERIAL);
Registry.register(Registries.ITEM, Identifier.of("mozc_caps", "caps"), CAPS);
Registry.register(Registries.ITEM, Identifier.of("mozc_caps", "caps_strapped"), CAPS_STRAPPED);

Networking.registerServer();
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/lx862/mozccaps/MainClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class MainClient implements ClientModInitializer {
public static HashMap<UUID, Double> keyPressedList = new HashMap<>();
public static HashMap<Integer, Double> keyPressedList = new HashMap<>();
public static final KeyBinding toggleInputKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.mozc_caps.toggle_input", GLFW.GLFW_KEY_Y, "category.mozc_caps.title"));

@Override
Expand Down Expand Up @@ -82,7 +81,7 @@ public static boolean capEquipped(boolean chinStrapped) {
}

private static void updateCapPressedAnimation(float delta) {
for(Map.Entry<UUID, Double> entry : new HashMap<>(keyPressedList).entrySet()) {
for(Map.Entry<Integer, Double> entry : new HashMap<>(keyPressedList).entrySet()) {
double newProgress = entry.getValue() + (delta / 8);
if(newProgress >= 1) {
keyPressedList.remove(entry.getKey());
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/com/lx862/mozccaps/armor/CapArmorRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.model.BipedEntityModel;
import net.minecraft.client.render.entity.state.BipedEntityRenderState;
import net.minecraft.client.render.entity.state.PlayerEntityRenderState;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
Expand All @@ -26,20 +28,7 @@ public CapArmorRenderer(boolean hasStrap) {
this.hasStrap = hasStrap;
}

@Override
public void render(MatrixStack matrices, VertexConsumerProvider vertexConsumers, ItemStack stack, LivingEntity entity, EquipmentSlot slot, int light, BipedEntityModel<LivingEntity> contextModel) {
VertexConsumer vertexConsumer = vertexConsumers.getBuffer(RenderLayer.getEntityCutout(TEXTURE_ID));
double animationProgress = MainClient.keyPressedList.getOrDefault(entity.getUuid(), 0.0);
double pressedAmount = animationProgress > 0.5 ? (1 - animationProgress) : (animationProgress);

renderCap(matrices, vertexConsumer, contextModel, pressedAmount, light);

if(hasStrap) {
renderStrap(matrices, vertexConsumer, contextModel, light);
}
}

private void renderCap(MatrixStack matrices, VertexConsumer vertexConsumer, BipedEntityModel<LivingEntity> contextModel, double pressedAmount, int light) {
private void renderCap(MatrixStack matrices, VertexConsumer vertexConsumer, BipedEntityModel<BipedEntityRenderState> contextModel, double pressedAmount, int light) {
Quaternionf rotation = new Quaternionf();
rotation.rotateX(CAP_TILT);

Expand All @@ -53,11 +42,26 @@ private void renderCap(MatrixStack matrices, VertexConsumer vertexConsumer, Bipe
matrices.pop();
}

private void renderStrap(MatrixStack matrices, VertexConsumer vertexConsumer, BipedEntityModel<LivingEntity> contextModel, int light) {
private void renderStrap(MatrixStack matrices, VertexConsumer vertexConsumer, BipedEntityModel<BipedEntityRenderState> contextModel, int light) {
matrices.push();
matrices.scale(0.6F, 0.6F, 0.6F);
chinModel.setTransform(contextModel.head.getTransform());
chinModel.render(matrices, vertexConsumer, light, OverlayTexture.DEFAULT_UV);
matrices.pop();
}

@Override
public void render(MatrixStack matrices, VertexConsumerProvider vertexConsumerProvider, ItemStack itemStack, BipedEntityRenderState bipedEntityRenderState, EquipmentSlot equipmentSlot, int light, BipedEntityModel<BipedEntityRenderState> contextModel) {
if(bipedEntityRenderState instanceof PlayerEntityRenderState) {
VertexConsumer vertexConsumer = vertexConsumerProvider.getBuffer(RenderLayer.getEntityCutout(TEXTURE_ID));
double animationProgress = MainClient.keyPressedList.getOrDefault(((PlayerEntityRenderState)bipedEntityRenderState).id, 0.0);
double pressedAmount = animationProgress > 0.5 ? (1 - animationProgress) : (animationProgress);

renderCap(matrices, vertexConsumer, contextModel, pressedAmount, light);

if(hasStrap) {
renderStrap(matrices, vertexConsumer, contextModel, light);
}
}
}
}
18 changes: 0 additions & 18 deletions src/main/java/com/lx862/mozccaps/armor/CapModel.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
package com.lx862.mozccaps.armor;

import com.lx862.mozccaps.Main;
import net.minecraft.client.model.*;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.recipe.Ingredient;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Util;

import java.util.EnumMap;

public class CapModel {

public static ArmorMaterial ARMOR_MATERIAL = new ArmorMaterial(Util.make(new EnumMap(ArmorItem.Type.class), map -> {
map.put(ArmorItem.Type.BODY, 1);
}),
15,
SoundEvents.ITEM_ARMOR_EQUIP_LEATHER,
() -> Ingredient.ofItems(Main.CAPS),
null,
0,
0);

/* Code generated from Blockbench */
public static TexturedModelData getTexturedModelData() {
ModelData modelData = new ModelData();
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/lx862/mozccaps/network/Networking.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void registerClient() {
PayloadTypeRegistry.playS2C().register(PlayerTypePayload.PACKET_ID, PlayerTypePayload.PACKET_CODEC);

ClientPlayNetworking.registerGlobalReceiver(PlayerTypePayload.PACKET_ID, (payload, context) -> {
UUID playerTyped = payload.getPlayerUuid();
int playerTyped = payload.getEntityId();
MainClient.keyPressedList.put(playerTyped, 0.0);
});
}
Expand All @@ -25,21 +25,21 @@ public static void registerServer() {
PayloadTypeRegistry.playC2S().register(PlayerTypePayload.PACKET_ID, PlayerTypePayload.PACKET_CODEC);

ServerPlayNetworking.registerGlobalReceiver(PlayerTypePayload.PACKET_ID, (payload, context) -> {
UUID playerUuid = payload.getPlayerUuid();
int playerId = payload.getEntityId();

MinecraftServer server = context.player().getServer();
if(server == null) return;

// Rebroadcast key pressed event to all player
server.getPlayerManager().getPlayerList().forEach(p -> {
ServerPlayNetworking.send(p, new PlayerTypePayload(playerUuid));
ServerPlayNetworking.send(p, new PlayerTypePayload(playerId));
});
});
}

public static void sendKeyPressedClient(PlayerEntity player) {
PacketByteBuf buf = PacketByteBufs.create();
buf.writeUuid(player.getUuid());
ClientPlayNetworking.send(new PlayerTypePayload(player.getUuid()));
ClientPlayNetworking.send(new PlayerTypePayload(player.getId()));
}
}
12 changes: 5 additions & 7 deletions src/main/java/com/lx862/mozccaps/network/PlayerTypePayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
import net.minecraft.util.Uuids;

import java.util.UUID;

public record PlayerTypePayload(UUID player) implements CustomPayload {
public record PlayerTypePayload(int entityId) implements CustomPayload {
public static final CustomPayload.Id<PlayerTypePayload> PACKET_ID = new CustomPayload.Id<>(Identifier.of("mozc_caps", "player_typed"));
public static final PacketCodec<RegistryByteBuf, PlayerTypePayload> PACKET_CODEC = Uuids.PACKET_CODEC.xmap(PlayerTypePayload::new, PlayerTypePayload::player).cast();
public static final PacketCodec<RegistryByteBuf, PlayerTypePayload> PACKET_CODEC = PacketCodec.tuple(PacketCodecs.INTEGER, PlayerTypePayload::entityId, PlayerTypePayload::new);

@Override
public Id<? extends CustomPayload> getId() {
return PACKET_ID;
}

public UUID getPlayerUuid() {
return player;
public int getEntityId() {
return entityId;
}
}

0 comments on commit 691e4ee

Please sign in to comment.