Skip to content

Commit

Permalink
Fix server crashing, fix Forge item name positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Jan 3, 2024
1 parent 98440b7 commit a0b9835
Show file tree
Hide file tree
Showing 29 changed files with 252 additions and 165 deletions.
6 changes: 3 additions & 3 deletions common/src/main/java/toughasnails/core/ToughAsNails.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void init()

public static void setupClient()
{
ModBlocks.registerRenderers();
ModClient.setupRenderTypes();
}

private static void addRegistrars()
Expand Down Expand Up @@ -84,8 +84,8 @@ private static void addClientHandlers()
EventManager.addListener(ThirstHandler::onClientTick);

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

// Tooltips
EventManager.addListener(TooltipHandler::onTooltip);
Expand Down
22 changes: 0 additions & 22 deletions common/src/main/java/toughasnails/init/ModBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
******************************************************************************/
package toughasnails.init;

import glitchcore.event.client.RegisterColorsEvent;
import glitchcore.util.RenderTypeHelper;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
Expand All @@ -21,9 +18,6 @@

import java.util.function.BiConsumer;

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

public class ModBlocks
{
public static void registerBlocks(BiConsumer<ResourceLocation, Block> func)
Expand All @@ -33,22 +27,6 @@ public static void registerBlocks(BiConsumer<ResourceLocation, Block> func)
TANBlocks.WATER_PURIFIER = register(func, "water_purifier", new WaterPurifierBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_ORANGE).requiresCorrectToolForDrops().strength(3.0F, 6.0F).sound(SoundType.COPPER).noOcclusion()));
}

public static void registerRenderers()
{
RenderType transparentRenderType = RenderType.cutoutMipped();
RenderType cutoutRenderType = RenderType.cutout();
RenderType translucentRenderType = RenderType.translucent();

RenderTypeHelper.setRenderType(RAIN_COLLECTOR, cutoutRenderType);
RenderTypeHelper.setRenderType(WATER_PURIFIER, cutoutRenderType);
}

public static void registerBlockColors(RegisterColorsEvent.Block event)
{
event.register((state, world, pos, tintIndex) -> 0x47DAFF, TANBlocks.RAIN_COLLECTOR);
event.register((state, world, pos, tintIndex) -> 0x3F76E4, TANBlocks.WATER_PURIFIER);
}

private static Block register(BiConsumer<ResourceLocation, Block> func, String name, Block block)
{
func.accept(new ResourceLocation(TANAPI.MOD_ID, name), block);
Expand Down
117 changes: 117 additions & 0 deletions common/src/main/java/toughasnails/init/ModClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*******************************************************************************
* Copyright 2023, the Glitchfiend Team.
* All rights reserved.
******************************************************************************/
package toughasnails.init;

import glitchcore.event.client.RegisterColorsEvent;
import glitchcore.util.RenderTypeHelper;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import toughasnails.api.item.TANItems;
import toughasnails.api.temperature.TemperatureHelper;
import toughasnails.api.temperature.TemperatureLevel;
import toughasnails.core.ToughAsNails;
import toughasnails.item.DyeableWoolItem;
import toughasnails.item.LeafArmorItem;

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

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

public class ModClient
{
static void registerItemProperties()
{
ItemProperties.register(TANItems.THERMOMETER, new ResourceLocation(ToughAsNails.MOD_ID, "temperature"), new ClampedItemPropertyFunction() {
final Map<Integer, Delta> deltas = new HashMap<>();


@Override
public float unclampedCall(ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int seed)
{
Entity holder = entity != null ? entity : stack.getEntityRepresentation();

if (holder == null)
return 0.5F;

if (level == null && holder.level() instanceof ClientLevel)
level = (ClientLevel)holder.level();

if (level == null)
return 0.5F;

Delta delta = deltas.computeIfAbsent(holder.getId(), k -> new Delta());
delta.update(level, TemperatureHelper.getTemperatureAtPos(level, holder.blockPosition()));
return delta.getValue();
}

private static class Delta
{
private long lastUpdateTick;
private double currentValue;
private double rota;

private void update(ClientLevel level, TemperatureLevel temperatureLevel)
{
if (level.getGameTime() == this.lastUpdateTick)
return;

this.lastUpdateTick = level.getGameTime();
double targetValue = temperatureLevel.ordinal() * 0.25;
double delta = targetValue - this.currentValue;

// Add a small increment to the rota to move towards the target value
this.rota += delta * 0.1;

// Diminish the rota over time. The clock uses 0.9, but we want slightly less wobbling
this.rota *= 0.87;
this.currentValue = Mth.clamp(this.currentValue + this.rota, 0.0, 1.0);
}

public float getValue()
{
// Round to the nearest 0.05
return (float)((double)Math.round(this.currentValue * 20.0) / 20.0);
}
}
});
}

public static void registerItemColors(RegisterColorsEvent.Item event)
{
event.register((stack, tintIndex) -> {
return tintIndex > 0 ? -1 : ((DyeableWoolItem)stack.getItem()).getColor(stack);
}, TANItems.WOOL_HELMET, TANItems.WOOL_CHESTPLATE, TANItems.WOOL_LEGGINGS, TANItems.WOOL_BOOTS);

event.register((stack, tintIndex) -> {
return tintIndex > 0 ? -1 : ((LeafArmorItem)stack.getItem()).getColor(stack);
}, TANItems.LEAF_HELMET, TANItems.LEAF_CHESTPLATE, TANItems.LEAF_LEGGINGS, TANItems.LEAF_BOOTS);
}

public static void registerBlockColors(RegisterColorsEvent.Block event)
{
event.register((state, world, pos, tintIndex) -> 0x47DAFF, RAIN_COLLECTOR);
event.register((state, world, pos, tintIndex) -> 0x3F76E4, WATER_PURIFIER);
}

public static void setupRenderTypes()
{
RenderType transparentRenderType = RenderType.cutoutMipped();
RenderType cutoutRenderType = RenderType.cutout();
RenderType translucentRenderType = RenderType.translucent();

RenderTypeHelper.setRenderType(RAIN_COLLECTOR, cutoutRenderType);
RenderTypeHelper.setRenderType(WATER_PURIFIER, cutoutRenderType);
}
}
88 changes: 4 additions & 84 deletions common/src/main/java/toughasnails/init/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,17 @@
******************************************************************************/
package toughasnails.init;

import glitchcore.event.client.RegisterColorsEvent;
import glitchcore.util.Environment;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.food.Foods;
import net.minecraft.world.item.*;
import org.jetbrains.annotations.Nullable;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import toughasnails.api.TANAPI;
import toughasnails.api.block.TANBlocks;
import toughasnails.api.item.TANItems;
import toughasnails.api.temperature.TemperatureHelper;
import toughasnails.api.temperature.TemperatureLevel;
import toughasnails.core.ToughAsNails;
import toughasnails.item.*;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class ModItems
Expand Down Expand Up @@ -98,78 +86,10 @@ public static void registerItems(BiConsumer<ResourceLocation, Item> func)

if (Environment.isClient())
{
registerItemProperties();
ModClient.registerItemProperties();
}
}

public static void registerItemColors(RegisterColorsEvent.Item event)
{
event.register((stack, tintIndex) -> {
return tintIndex > 0 ? -1 : ((DyeableWoolItem)stack.getItem()).getColor(stack);
}, TANItems.WOOL_HELMET, TANItems.WOOL_CHESTPLATE, TANItems.WOOL_LEGGINGS, TANItems.WOOL_BOOTS);

event.register((stack, tintIndex) -> {
return tintIndex > 0 ? -1 : ((LeafArmorItem)stack.getItem()).getColor(stack);
}, TANItems.LEAF_HELMET, TANItems.LEAF_CHESTPLATE, TANItems.LEAF_LEGGINGS, TANItems.LEAF_BOOTS);
}

private static void registerItemProperties()
{
ItemProperties.register(TANItems.THERMOMETER, new ResourceLocation(ToughAsNails.MOD_ID, "temperature"), new ClampedItemPropertyFunction() {
final Map<Integer, Delta> deltas = new HashMap<>();


@Override
public float unclampedCall(ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int seed)
{
Entity holder = entity != null ? entity : stack.getEntityRepresentation();

if (holder == null)
return 0.5F;

if (level == null && holder.level() instanceof ClientLevel)
level = (ClientLevel)holder.level();

if (level == null)
return 0.5F;

Delta delta = deltas.computeIfAbsent(holder.getId(), k -> new Delta());
delta.update(level, TemperatureHelper.getTemperatureAtPos(level, holder.blockPosition()));
return delta.getValue();
}

private static class Delta
{
private long lastUpdateTick;
private double currentValue;
private double rota;

private void update(ClientLevel level, TemperatureLevel temperatureLevel)
{
if (level.getGameTime() == this.lastUpdateTick)
return;

this.lastUpdateTick = level.getGameTime();
double targetValue = temperatureLevel.ordinal() * 0.25;
double delta = targetValue - this.currentValue;

// Add a small increment to the rota to move towards the target value
this.rota += delta * 0.1;

// Diminish the rota over time. The clock uses 0.9, but we want slightly less wobbling
this.rota *= 0.87;
this.currentValue = Mth.clamp(this.currentValue + this.rota, 0.0, 1.0);
}

public float getValue()
{
// Round to the nearest 0.05
return (float)((double)Math.round(this.currentValue * 20.0) / 20.0);
}
}
});
}

private static Item register(BiConsumer<ResourceLocation, Item> func, String name, Item item)
{
func.accept(new ResourceLocation(TANAPI.MOD_ID, name), item);
Expand Down
17 changes: 3 additions & 14 deletions common/src/main/java/toughasnails/mixin/client/MixinGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import net.minecraft.client.gui.GuiGraphics;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Group;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Surrogate;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import toughasnails.core.ToughAsNails;
import toughasnails.temperature.TemperatureHooksClient;

@Mixin(Gui.class)
Expand All @@ -21,18 +24,4 @@ public void onRenderHeart(GuiGraphics gui, Gui.HeartType heartType, int x, int y
TemperatureHooksClient.heartBlit(gui, heartType, x, y, isHardcore, isBlinking, isHalf);
ci.cancel();
}

@Inject(method="renderSelectedItemName", at=@At(value="HEAD"))
public void onRenderSelectedItemNameBegin(GuiGraphics guiGraphics, CallbackInfo ci)
{
var pose = guiGraphics.pose();
pose.pushPose();
TemperatureHooksClient.adjustSelectedItemText(guiGraphics);
}

@Inject(method="renderSelectedItemName", at=@At(value="TAIL"))
public void onRenderSelectedItemNameEnd(GuiGraphics guiGraphics, CallbackInfo ci)
{
guiGraphics.pose().popPose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
******************************************************************************/
package toughasnails.temperature;

import glitchcore.util.GuiUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiGraphics;
Expand Down Expand Up @@ -43,8 +44,8 @@ public static void adjustSelectedItemText(GuiGraphics guiGraphics)
{
var pose = guiGraphics.pose();

// If temperature is enabled, move the selected item text up by 2 pixels
if (ModConfig.temperature.enableTemperature)
// If temperature is enabled, move the selected item text up by 2 pixels. This is only done in survival mode.
if (ModConfig.temperature.enableTemperature && GuiUtils.shouldDrawSurvivalElements())
{
pose.translate(0F, -2F, 0F);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* Copyright 2023, the Glitchfiend Team.
* All rights reserved.
******************************************************************************/
package toughasnails.core;
package toughasnails.fabric.core;

import glitchcore.fabric.GlitchCoreFabric;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import toughasnails.core.ToughAsNails;

public class ToughAsNailsFabric implements ModInitializer, ClientModInitializer
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright 2023, the Glitchfiend Team.
* All rights reserved.
******************************************************************************/
package toughasnails.fabric.mixin.client;

import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiGraphics;
import org.spongepowered.asm.mixin.Mixin;
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.TemperatureHooksClient;

@Mixin(Gui.class)
public class MixinGui
{
@Inject(method="renderSelectedItemName", at=@At(value="HEAD"))
public void onRenderSelectedItemNameBegin(GuiGraphics guiGraphics, CallbackInfo ci)
{
guiGraphics.pose().pushPose();
TemperatureHooksClient.adjustSelectedItemText(guiGraphics);
}

@Inject(method="renderSelectedItemName", at=@At(value="TAIL"))
public void onRenderSelectedItemNameEnd(GuiGraphics guiGraphics, CallbackInfo ci)
{
guiGraphics.pose().popPose();
}
}
Loading

0 comments on commit a0b9835

Please sign in to comment.