Skip to content

Commit

Permalink
Now runs on servers properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Dec 31, 2023
1 parent ef06d6e commit 3eac6fd
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 84 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/1-bug_report.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: 1.19+ Bug Report
name: 1.20.4+ Bug Report
description: File a bug report
labels: [bug]
body:
- type: markdown
attributes:
value: |
For bugs experienced with Minecraft 1.19+. Older versions are not supported.
For bugs experienced with Minecraft 1.20.4+. Older versions are not supported.
If any section does not apply, replace its content with "N/A".
Please search for existing bug reports before making your own report.
Expand Down
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/2-crash_report.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: 1.19+ Crash Report
name: 1.20.4+ Crash Report
description: File a crash report
labels: [crash]
body:
- type: markdown
attributes:
value: |
For crashes experienced with Minecraft 1.19+. Older versions are not supported.
For crashes experienced with Minecraft 1.20.4+. Older versions are not supported.
If any section does not apply, replace its content with "N/A".
Please search for existing crash reports before making your own report.
Expand Down
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/3-feature_request.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: 1.19+ Feature Request
name: 1.20.4+ Feature Request
description: Request a new feature
labels: [feature]
body:
- type: markdown
attributes:
value: |
Feature requests for Minecraft 1.19+. Older versions are not supported.
Feature requests for Minecraft 1.20.4+. Older versions are not supported.
If any section does not apply, replace its contents with "N/A".
Please search for existing feature requests before you make your own request.
Expand Down
31 changes: 17 additions & 14 deletions common/src/main/java/toughasnails/core/ToughAsNails.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
package toughasnails.core;

import glitchcore.event.EventManager;
import glitchcore.util.Environment;
import glitchcore.util.RegistryHelper;
import glitchcore.util.RenderTypeHelper;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.registries.Registries;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -21,9 +17,6 @@
import toughasnails.thirst.ThirstHandler;
import toughasnails.thirst.ThirstOverlayRenderer;

import static toughasnails.api.block.TANBlocks.RAIN_COLLECTOR;
import static toughasnails.api.block.TANBlocks.WATER_PURIFIER;

public class ToughAsNails
{
public static final String MOD_ID = TANAPI.MOD_ID;
Expand All @@ -39,9 +32,10 @@ public static void init()
ModApi.init();
}

public static void initClient()
public static void setupClient()
{
ModBlocks.registerRenderers();
addClientHandlers();
}

private static void addRegistrars()
Expand All @@ -62,23 +56,32 @@ private static void addRegistrars()
private static void addHandlers()
{
// Temperature handlers
EventManager.addListener(TemperatureOverlayRenderer::onClientTick);
EventManager.addListener(TemperatureOverlayRenderer::onBeginRenderFood);
EventManager.addListener(TemperatureOverlayRenderer::onBeginRenderFrostbite);
EventManager.addListener(TemperatureHandler::onChangeDimension);

// Thirst handlers
EventManager.addListener(ThirstOverlayRenderer::onClientTick);
EventManager.addListener(ThirstOverlayRenderer::onBeginRenderAir);
EventManager.addListener(ThirstHandler::onChangeDimension);
EventManager.addListener(ThirstHandler::onItemUseFinish);
EventManager.addListener(ThirstHandler::onPlayerUseItem);
}

private static void addClientHandlers()
{
// Temperature
EventManager.addListener(TemperatureOverlayRenderer::onClientTick);
EventManager.addListener(TemperatureOverlayRenderer::onBeginRenderFood);
EventManager.addListener(TemperatureOverlayRenderer::onBeginRenderFrostbite);

// Thirst
EventManager.addListener(ThirstOverlayRenderer::onClientTick);
EventManager.addListener(ThirstOverlayRenderer::onBeginRenderAir);
EventManager.addListener(ThirstHandler::onUseEmpty);
EventManager.addListener(ThirstHandler::onClientTick);

// Client handlers
// Coloring
EventManager.addListener(ModBlocks::registerBlockColors);
EventManager.addListener(ModItems::registerItemColors);

// Tooltips
EventManager.addListener(TooltipHandler::onTooltip);
EventManager.addListener(TooltipHandler::onRenderTooltip);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
******************************************************************************/
package toughasnails.init;

import glitchcore.util.Environment;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.flag.FeatureFlags;
Expand All @@ -21,7 +22,11 @@ public class ModContainerTypes
public static void registerContainers(BiConsumer<ResourceLocation, MenuType<?>> func)
{
TANContainerTypes.WATER_PURIFIER = register(func, "water_purifier", WaterPurifierContainer::new);
MenuScreens.register((MenuType<WaterPurifierContainer>)TANContainerTypes.WATER_PURIFIER, WaterPurifierScreen::new);

if (Environment.isClient())
{
MenuScreens.register((MenuType<WaterPurifierContainer>) TANContainerTypes.WATER_PURIFIER, WaterPurifierScreen::new);
}
}

public static <T extends AbstractContainerMenu> MenuType<?> register(BiConsumer<ResourceLocation, MenuType<?>> func, String name, MenuType.MenuSupplier<T> factory)
Expand Down
13 changes: 10 additions & 3 deletions common/src/main/java/toughasnails/init/ModPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
******************************************************************************/
package toughasnails.init;

import glitchcore.network.CustomPacket;
import glitchcore.network.PacketHandler;
import net.minecraft.resources.ResourceLocation;
import toughasnails.api.TANAPI;
import toughasnails.core.ToughAsNails;
import toughasnails.network.DrinkInWorldPacket;
import toughasnails.network.UpdateTemperaturePacket;
import toughasnails.network.UpdateThirstPacket;
Expand All @@ -17,8 +19,13 @@ public class ModPackets
public static final PacketHandler HANDLER = new PacketHandler(CHANNEL);
public static void init()
{
HANDLER.register(new UpdateTemperaturePacket());
HANDLER.register(new UpdateThirstPacket());
HANDLER.register(new DrinkInWorldPacket());
register("update_temperature", new UpdateTemperaturePacket());
register("update_thirst", new UpdateThirstPacket());
register("drink_in_world", new DrinkInWorldPacket());
}

public static void register(String name, CustomPacket<?> packet)
{
HANDLER.register(new ResourceLocation(ToughAsNails.MOD_ID, name), packet);
}
}
4 changes: 2 additions & 2 deletions common/src/main/java/toughasnails/mixin/client/MixinGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import toughasnails.temperature.TemperatureHooks;
import toughasnails.temperature.TemperatureHooksClient;

@Mixin(Gui.class)
public abstract class MixinGui
{
@Inject(method="renderHeart", at=@At(value="HEAD"), cancellable = true)
public void onRenderHeart(GuiGraphics gui, Gui.HeartType heartType, int x, int y, boolean isHardcore, boolean isBlinking, boolean isHalf, CallbackInfo ci)
{
TemperatureHooks.heartBlit(gui, heartType, x, y, isHardcore, isBlinking, isHalf);
TemperatureHooksClient.heartBlit(gui, heartType, x, y, isHardcore, isBlinking, isHalf);
ci.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import toughasnails.thirst.ThirstHooks;
import toughasnails.thirst.ThirstHooksClient;

@Mixin(LocalPlayer.class)
public class MixinLocalPlayer
{
@Redirect(method="aiStep", at=@At(value="INVOKE", target="Lnet/minecraft/client/player/LocalPlayer;setSprinting(Z)V"))
public void aiStep_setSprinting(LocalPlayer player, boolean sprinting)
{
ThirstHooks.onAiStepSetSprinting(player, sprinting);
ThirstHooksClient.onAiStepSetSprinting(player, sprinting);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import toughasnails.api.potion.TANEffects;
import toughasnails.api.temperature.TemperatureLevel;
import toughasnails.api.thirst.IThirst;
import toughasnails.api.thirst.ThirstHelper;
import toughasnails.init.ModConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import toughasnails.api.temperature.ITemperature;
import toughasnails.api.temperature.TemperatureHelper;
import toughasnails.api.temperature.TemperatureLevel;
import toughasnails.api.thirst.IThirst;
import toughasnails.api.thirst.ThirstHelper;
import toughasnails.core.ToughAsNails;

public class UpdateTemperaturePacket implements CustomPacket<UpdateTemperaturePacket>
{
Expand Down Expand Up @@ -47,10 +44,19 @@ public void handle(UpdateTemperaturePacket packet, Context context)
if (context.isServerSide())
return;

Player player = Minecraft.getInstance().player;
ITemperature temperature = TemperatureHelper.getTemperatureData(player);
Detail.setTemperatureClient(packet.temperatureLevel, packet.hyperthermiaTicks);
}

temperature.setLevel(packet.temperatureLevel);
temperature.setHyperthermiaTicks(packet.hyperthermiaTicks);
// Used to isolate client code from the server
private static class Detail
{
private static void setTemperatureClient(TemperatureLevel level, int hyperthermiaTicks)
{
Player player = Minecraft.getInstance().player;
ITemperature temperature = TemperatureHelper.getTemperatureData(player);

temperature.setLevel(level);
temperature.setHyperthermiaTicks(hyperthermiaTicks);
}
}
}
20 changes: 16 additions & 4 deletions common/src/main/java/toughasnails/network/UpdateThirstPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import toughasnails.api.temperature.ITemperature;
import toughasnails.api.temperature.TemperatureHelper;
import toughasnails.api.temperature.TemperatureLevel;
import toughasnails.api.thirst.IThirst;
import toughasnails.api.thirst.ThirstHelper;

Expand Down Expand Up @@ -43,10 +46,19 @@ public void handle(UpdateThirstPacket packet, Context context)
if (context.isServerSide())
return;

Player player = Minecraft.getInstance().player;
IThirst thirst = ThirstHelper.getThirst(player);
Detail.setThirstClient(packet.thirstLevel, packet.hydrationLevel);
}

// Used to isolate client code from the server
private static class Detail
{
private static void setThirstClient(int thirstLevel, float hydrationLevel)
{
Player player = Minecraft.getInstance().player;
IThirst thirst = ThirstHelper.getThirst(player);

thirst.setThirst(packet.thirstLevel);
thirst.setHydration(packet.hydrationLevel);
thirst.setThirst(thirstLevel);
thirst.setHydration(hydrationLevel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.resources.ResourceLocation;
import toughasnails.api.temperature.TemperatureHelper;

public class TemperatureHooks
public class TemperatureHooksClient
{
private static final ResourceLocation OVERHEATED_HEART_FULL = new ResourceLocation("toughasnails:hud/heart/overheated_full");
private static final ResourceLocation OVERHEATED_HEART_FULL_BLINKING = new ResourceLocation("toughasnails:hud/heart/overheated_full_blinking");
Expand Down
34 changes: 0 additions & 34 deletions common/src/main/java/toughasnails/thirst/ThirstHooks.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package toughasnails.thirst;

import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.Difficulty;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodData;
Expand Down Expand Up @@ -90,37 +89,4 @@ else if (data.foodLevel <= 0)
}
}

public static void onAiStep(LocalPlayer player)
{
if (player.isSprinting())
{
boolean sprintingAllowable = canSprintWithThirst(player);

if (player.isSwimming())
{
if (!player.onGround() && !player.input.shiftKeyDown && !sprintingAllowable || !player.isInWater())
{
player.setSprinting(false);
}
}
else if (!sprintingAllowable)
{
player.setSprinting(false);
}
}
}

public static void onAiStepSetSprinting(LocalPlayer player, boolean sprinting)
{
// Don't allow sprinting if the player has insufficient thirst
if (sprinting && !canSprintWithThirst(player))
sprinting = false;

player.setSprinting(sprinting);
}

private static boolean canSprintWithThirst(LocalPlayer player)
{
return ThirstHelper.getThirst(player).getThirst() > 6 || player.getAbilities().mayfly;
}
}
45 changes: 45 additions & 0 deletions common/src/main/java/toughasnails/thirst/ThirstHooksClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*******************************************************************************
* Copyright 2023, the Glitchfiend Team.
* All rights reserved.
******************************************************************************/
package toughasnails.thirst;

import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.entity.player.Player;
import toughasnails.api.thirst.ThirstHelper;

public class ThirstHooksClient {
public static void onAiStep(LocalPlayer player)
{
if (player.isSprinting())
{
boolean sprintingAllowable = canSprintWithThirst(player);

if (player.isSwimming())
{
if (!player.onGround() && !player.input.shiftKeyDown && !sprintingAllowable || !player.isInWater())
{
player.setSprinting(false);
}
}
else if (!sprintingAllowable)
{
player.setSprinting(false);
}
}
}

public static void onAiStepSetSprinting(LocalPlayer player, boolean sprinting)
{
// Don't allow sprinting if the player has insufficient thirst
if (sprinting && !canSprintWithThirst(player))
sprinting = false;

player.setSprinting(sprinting);
}

private static boolean canSprintWithThirst(LocalPlayer player)
{
return ThirstHelper.getThirst(player).getThirst() > 6 || player.getAbilities().mayfly;
}
}
Loading

0 comments on commit 3eac6fd

Please sign in to comment.