diff --git a/src/main/java/com/sekwah/narutomod/capabilities/INinjaData.java b/src/main/java/com/sekwah/narutomod/capabilities/INinjaData.java index cf026177..58411045 100644 --- a/src/main/java/com/sekwah/narutomod/capabilities/INinjaData.java +++ b/src/main/java/com/sekwah/narutomod/capabilities/INinjaData.java @@ -5,6 +5,7 @@ import net.minecraft.nbt.Tag; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.util.INBTSerializable; import java.util.HashMap; @@ -26,6 +27,8 @@ public interface INinjaData extends INBTSerializable { void addChakra(float amount); void addStamina(float amount); + Vec3 getSubstitutionLoc(); + DoubleJumpData getDoubleJumpData(); HashMap getCooldownEvents(); diff --git a/src/main/java/com/sekwah/narutomod/capabilities/NinjaData.java b/src/main/java/com/sekwah/narutomod/capabilities/NinjaData.java index 0277b84a..b722ebf7 100644 --- a/src/main/java/com/sekwah/narutomod/capabilities/NinjaData.java +++ b/src/main/java/com/sekwah/narutomod/capabilities/NinjaData.java @@ -14,6 +14,7 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundSource; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.LazyOptional; @@ -33,7 +34,7 @@ public class NinjaData implements INinjaData, ICapabilityProvider { @Sync(minTicks = 1) private float stamina; - @Sync + @Sync(minTicks = 1) private float substitutions; @Sync @@ -42,9 +43,12 @@ public class NinjaData implements INinjaData, ICapabilityProvider { @Sync private float maxStamina; - @Sync + // Unless the player needs to know the max for rendering, no point in rendering. private float maxSubstitutions; + @Sync + private Vec3 substitutionLocation; + /** * If the player can double jump, will be updated by underlying values server side. * @@ -112,6 +116,7 @@ public boolean canRegen() { private static final String STAMINA_TAG = "stamina"; private static final String SAVE_TIME = "save_time"; private static final String COOLDOWN_TAG = "cooldowns"; + private static final String SUBSTITUTION_TAG = "substitutions"; private final LazyOptional holder = LazyOptional.of(() -> this); @@ -177,6 +182,11 @@ public void addStamina(float amount) { this.stamina = Math.min(Math.max(this.stamina + amount, 0), maxStamina); } + @Override + public Vec3 getSubstitutionLoc() { + return this.substitutionLocation; + } + @Override public DoubleJumpData getDoubleJumpData() { return this.doubleJumpData; @@ -314,6 +324,7 @@ public Tag serializeNBT() { cooldownData.putInt(key, event.ticks); } nbt.put(COOLDOWN_TAG, cooldownData); + nbt.putFloat(SUBSTITUTION_TAG, this.substitutions); return nbt; } @@ -329,6 +340,7 @@ public void deserializeNBT(Tag tag) { for (String key : cooldownData.getAllKeys()) { this.cooldownTickEvents.put(key, new CooldownTickEvent(cooldownData.getInt(key) - ticksPassed)); } + this.substitutions = compoundTag.getFloat(SUBSTITUTION_TAG); } } diff --git a/src/main/java/com/sekwah/narutomod/client/gui/ChakraAndStaminaGUI.java b/src/main/java/com/sekwah/narutomod/client/gui/ChakraAndStaminaGUI.java index 266fbc81..ee45cad1 100644 --- a/src/main/java/com/sekwah/narutomod/client/gui/ChakraAndStaminaGUI.java +++ b/src/main/java/com/sekwah/narutomod/client/gui/ChakraAndStaminaGUI.java @@ -24,11 +24,6 @@ public class ChakraAndStaminaGUI extends GuiComponent { private int screenWidth; private int screenHeight; - /** - * Will be false if the entity is not a ninja - */ - private boolean shouldRender; - private float chakra; private float stamina; @@ -40,9 +35,6 @@ public ChakraAndStaminaGUI(Minecraft mc) { } public void render(PoseStack matrixStack) { - if(!shouldRender) { - return; - } this.screenWidth = this.minecraft.getWindow().getGuiScaledWidth(); this.screenHeight = this.minecraft.getWindow().getGuiScaledHeight(); int barDesign = NarutoConfig.chakraBarDesign; @@ -137,20 +129,13 @@ private void centeredTextOutlined(PoseStack matrixStack, String text, int x, int this.getFont().draw(matrixStack, text, (float) x - width, y, color); } - public void tick() { - if(this.minecraft.getCameraEntity() instanceof Player player) { - shouldRender = true; - if(player != null) { - player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> { - chakra = ninjaData.getChakra(); - stamina = ninjaData.getStamina(); - maxChakra = ninjaData.getMaxChakra(); - maxStamina = ninjaData.getMaxStamina(); - }); - } - } else { - shouldRender = false; - } + public void tick(Player player) { + player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> { + this.chakra = ninjaData.getChakra(); + this.stamina = ninjaData.getStamina(); + this.maxChakra = ninjaData.getMaxChakra(); + this.maxStamina = ninjaData.getMaxStamina(); + }); } private void setColor(Color color) { diff --git a/src/main/java/com/sekwah/narutomod/client/gui/NarutoInGameGUI.java b/src/main/java/com/sekwah/narutomod/client/gui/NarutoInGameGUI.java index 2582ad19..fec6fa20 100644 --- a/src/main/java/com/sekwah/narutomod/client/gui/NarutoInGameGUI.java +++ b/src/main/java/com/sekwah/narutomod/client/gui/NarutoInGameGUI.java @@ -1,7 +1,9 @@ package com.sekwah.narutomod.client.gui; import com.mojang.blaze3d.vertex.PoseStack; +import com.sekwah.narutomod.capabilities.NinjaCapabilityHandler; import net.minecraft.client.Minecraft; +import net.minecraft.world.entity.player.Player; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.TickEvent; @@ -10,17 +12,31 @@ public class NarutoInGameGUI { private final ChakraAndStaminaGUI charkaOverlay; + private final SubstitutionGUI substitutionOverlay; + private final Minecraft minecraft; + + /** + * Will be false if the entity is not a ninja + */ + private boolean shouldRender; public NarutoInGameGUI(){ - Minecraft mc = Minecraft.getInstance(); - this.charkaOverlay = new ChakraAndStaminaGUI(mc); + this.minecraft = Minecraft.getInstance(); + this.charkaOverlay = new ChakraAndStaminaGUI(this.minecraft); + this.substitutionOverlay = new SubstitutionGUI(this.minecraft); MinecraftForge.EVENT_BUS.addListener(this::renderGameOverlay); MinecraftForge.EVENT_BUS.addListener(this::clientTickEvent); } @SubscribeEvent public void clientTickEvent(TickEvent.ClientTickEvent event) { - this.charkaOverlay.tick(); + if(this.minecraft.getCameraEntity() instanceof Player player) { + shouldRender = true; + this.charkaOverlay.tick(player); + this.substitutionOverlay.tick(player); + } else { + shouldRender = false; + } } @SubscribeEvent @@ -28,7 +44,11 @@ public void renderGameOverlay(RenderGameOverlayEvent event) { if(event.getType() != RenderGameOverlayEvent.ElementType.ALL) { return; } + if(!shouldRender) { + return; + } PoseStack stack = event.getMatrixStack(); this.charkaOverlay.render(stack); + this.substitutionOverlay.render(stack); } } diff --git a/src/main/java/com/sekwah/narutomod/client/gui/SubstitutionGUI.java b/src/main/java/com/sekwah/narutomod/client/gui/SubstitutionGUI.java new file mode 100644 index 00000000..e9c57c3d --- /dev/null +++ b/src/main/java/com/sekwah/narutomod/client/gui/SubstitutionGUI.java @@ -0,0 +1,110 @@ +package com.sekwah.narutomod.client.gui; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; +import com.sekwah.narutomod.NarutoMod; +import com.sekwah.narutomod.capabilities.NinjaCapabilityHandler; +import com.sekwah.narutomod.config.NarutoConfig; +import com.sekwah.narutomod.util.ColorUtil; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiComponent; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Player; + +import java.awt.*; + +public class SubstitutionGUI extends GuiComponent { + + private static final ResourceLocation LOG_TEXTURE = new ResourceLocation(NarutoMod.MOD_ID, "textures/gui/jutsu/jutsu_substiutution.png"); + + private final Minecraft minecraft; + private int screenWidth; + private int screenHeight; + + private final int intStaminaColor; + private final int intChakraColor; + + private float substitutions; + + public SubstitutionGUI(Minecraft mc) { + this.minecraft = mc; + + Color chakraColor = new Color(255, 255, 255); + Color chakraColorDarker = new Color(0, 0, 0); + this.intStaminaColor = ColorUtil.toMCColor(chakraColor).getValue(); + this.intChakraColor = ColorUtil.toMCColor(chakraColorDarker).getValue(); + } + + public void render(PoseStack matrixStack) { + this.screenWidth = this.minecraft.getWindow().getGuiScaledWidth(); + this.screenHeight = this.minecraft.getWindow().getGuiScaledHeight(); + int screenMid = this.screenWidth / 2; + + int xPos = screenMid + 102; + int yOffset = 23; + float brightness = 0.4f; + + int textureWidth = 19; + int textureHeight = 18; + + float height = this.substitutions % 1; + + if(height == 0) { + height = 1; + } + + int pixelHeight = Math.round(textureHeight * height); + + // stack, x, y, tx, ty, width, height, textureWidth, textureHeight + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderTexture(0, LOG_TEXTURE); + RenderSystem.setShaderColor(brightness, brightness, brightness, 1.0F); + blit(matrixStack, xPos, this.screenHeight - yOffset, + 6, 7, + textureWidth, textureHeight, + 32, 32); + RenderSystem.setShaderColor(1.0f, 1.0f,1.0f,1.0F); + blit(matrixStack, xPos, this.screenHeight - yOffset + textureHeight - pixelHeight, + 6, 7 + textureHeight - pixelHeight, + textureWidth, pixelHeight, + 32, 32); + + + + this.centeredTextOutlined(matrixStack, + String.valueOf((int) Math.floor(substitutions)), + xPos + 15, + this.screenHeight - yOffset + 12, + intStaminaColor, + intChakraColor); + + } + + private void centeredTextOutlined(PoseStack matrixStack, String text, int x, int y, int color, int backgroundColor) { + int width = this.getFont().width(text) / 2; + this.getFont().draw(matrixStack, text, (float) x+1 - width, y, backgroundColor); + this.getFont().draw(matrixStack, text, (float) x-1 - width, y, backgroundColor); + this.getFont().draw(matrixStack, text, (float) x - width, (float) y+1, backgroundColor); + this.getFont().draw(matrixStack, text, (float) x - width, (float) y-1, backgroundColor); + this.getFont().draw(matrixStack, text, (float) x - width, y, color); + } + + public void tick(Player player) { + player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> { + this.substitutions = ninjaData.getSubstitutionCount(); + }); + } + + private void setColor(Color color) { + RenderSystem.setShaderColor(color.getRed() / 255f, + color.getGreen() / 255f, + color.getBlue() / 255f, + 1.0F); + } + + private Font getFont() { + return this.minecraft.font; + } +} diff --git a/src/main/java/com/sekwah/narutomod/config/NarutoConfig.java b/src/main/java/com/sekwah/narutomod/config/NarutoConfig.java index 9735ad6b..eed4cd5c 100644 --- a/src/main/java/com/sekwah/narutomod/config/NarutoConfig.java +++ b/src/main/java/com/sekwah/narutomod/config/NarutoConfig.java @@ -96,7 +96,7 @@ public class NarutoConfig { .defineInRange("maxSubstitutions", 3 , 0, Integer.MAX_VALUE); CONFIG_SUBSTITUTION_REGEN_TIME = configBuilder.comment("Substitution Regen Time (Seconds)") - .defineInRange("substitutionRegenRate", 60 , 0, Integer.MAX_VALUE); + .defineInRange("substitutionRegenTime", 60 , 0, Integer.MAX_VALUE); configBuilder.pop(); diff --git a/src/main/java/com/sekwah/sekclib/capabilitysync/capabilitysync/tracker/implemented/Vec3SyncTracker.java b/src/main/java/com/sekwah/sekclib/capabilitysync/capabilitysync/tracker/implemented/Vec3SyncTracker.java new file mode 100644 index 00000000..81889a6f --- /dev/null +++ b/src/main/java/com/sekwah/sekclib/capabilitysync/capabilitysync/tracker/implemented/Vec3SyncTracker.java @@ -0,0 +1,33 @@ +package com.sekwah.sekclib.capabilitysync.capabilitysync.tracker.implemented; + +import com.sekwah.sekclib.capabilitysync.capabilitysync.tracker.SyncTrackerSerializer; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.world.phys.Vec3; + +/** + * Sync structure + * boolean isNull + * double posX + * double posX + * double posX + */ +public class Vec3SyncTracker implements SyncTrackerSerializer { + + @Override + public void encode(Vec3 objectToSend, FriendlyByteBuf outBuffer) { + outBuffer.writeBoolean(objectToSend == null); + if(objectToSend != null) { + outBuffer.writeDouble(objectToSend.x); + outBuffer.writeDouble(objectToSend.y); + outBuffer.writeDouble(objectToSend.z); + } + } + + @Override + public Vec3 decode(FriendlyByteBuf inBuffer) { + if(inBuffer.readBoolean()) { + return null; + } + return new Vec3(inBuffer.readDouble(), inBuffer.readDouble(), inBuffer.readDouble()); + } +} diff --git a/src/main/resources/assets/narutomod/textures/jutsu/jutsu_substiutution.png b/src/main/resources/assets/narutomod/textures/gui/jutsu/jutsu_substiutution.png similarity index 100% rename from src/main/resources/assets/narutomod/textures/jutsu/jutsu_substiutution.png rename to src/main/resources/assets/narutomod/textures/gui/jutsu/jutsu_substiutution.png