Skip to content

Commit eb890f5

Browse files
committed
Fix Railgun item crash related to temperature colour
1 parent b404b52 commit eb890f5

1 file changed

Lines changed: 108 additions & 98 deletions

File tree

  • src/main/java/electrodynamics/common/item/gear/tools/electric/utils

src/main/java/electrodynamics/common/item/gear/tools/electric/utils/ItemRailgun.java

Lines changed: 108 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -35,116 +35,126 @@
3535

3636
public abstract class ItemRailgun extends ItemElectric implements IItemTemperate {
3737

38-
private static final List<ItemRailgun> ITEMS = new ArrayList<>();
39-
40-
public static final Color[] HEAT_COLORS = {new Color(183, 183, 183, 255), new Color(102, 0, 0, 255), new Color(152, 1, 0, 255), new Color(204, 0 ,1, 255), new Color(253, 51, 1, 255), new Color(255, 102, 51, 255), new Color(254, 154, 100, 255), new Color(255, 203, 102, 255), new Color(254, 204, 50, 255), new Color(255, 255, 101, 255), new Color(255, 255, 153, 255)};
41-
42-
public static final int CAPACITY = 5000;
43-
44-
private final TemperateItemProperties temperateProperties = new TemperateItemProperties();
45-
private double overheatTemperature = 0;
46-
private double tempThreshold = 0;
47-
private double tempPerTick = 0;
48-
49-
public ItemRailgun(ElectricItemProperties properties, Holder<CreativeModeTab> creativeTab, double overheatTemperature, double tempThreshold, double tempPerTick, Function<Item, Item> getBatteryItem) {
50-
super(properties, creativeTab, getBatteryItem);
51-
this.overheatTemperature = overheatTemperature;
52-
this.tempThreshold = tempThreshold;
53-
this.tempPerTick = tempPerTick;
54-
ITEMS.add(this);
38+
private static final List<ItemRailgun> ITEMS = new ArrayList<>();
39+
40+
public static final Color[] HEAT_COLORS = { new Color(183, 183, 183, 255), new Color(102, 0, 0, 255),
41+
new Color(152, 1, 0, 255), new Color(204, 0, 1, 255), new Color(253, 51, 1, 255),
42+
new Color(255, 102, 51, 255), new Color(254, 154, 100, 255), new Color(255, 203, 102, 255),
43+
new Color(254, 204, 50, 255), new Color(255, 255, 101, 255), new Color(255, 255, 153, 255) };
44+
45+
public static final int CAPACITY = 5000;
46+
47+
private final TemperateItemProperties temperateProperties = new TemperateItemProperties();
48+
private double overheatTemperature = 0;
49+
private double tempThreshold = 0;
50+
private double tempPerTick = 0;
51+
52+
public ItemRailgun(ElectricItemProperties properties, Holder<CreativeModeTab> creativeTab,
53+
double overheatTemperature, double tempThreshold, double tempPerTick, Function<Item, Item> getBatteryItem) {
54+
super(properties, creativeTab, getBatteryItem);
55+
this.overheatTemperature = overheatTemperature;
56+
this.tempThreshold = tempThreshold;
57+
this.tempPerTick = tempPerTick;
58+
ITEMS.add(this);
59+
}
60+
61+
@Override
62+
public void setDamage(ItemStack stack, int damage) {
63+
64+
}
65+
66+
@Override
67+
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltip, TooltipFlag flagIn) {
68+
super.appendHoverText(stack, context, tooltip, flagIn);
69+
tooltip.add(ElectroTextUtils.tooltip("railguntemp", ChatFormatter
70+
.getChatDisplayShort(IItemTemperate.getTemperature(stack), DisplayUnits.TEMPERATURE_CELCIUS))
71+
.withStyle(ChatFormatting.YELLOW));
72+
tooltip.add(ElectroTextUtils
73+
.tooltip("railgunmaxtemp",
74+
ChatFormatter.getChatDisplayShort(overheatTemperature, DisplayUnits.TEMPERATURE_CELCIUS))
75+
.withStyle(ChatFormatting.YELLOW));
76+
if (IItemTemperate.getTemperature(stack) >= getOverheatTemp()) {
77+
tooltip.add(ElectroTextUtils.tooltip("railgunoverheat").withStyle(ChatFormatting.RED, ChatFormatting.BOLD));
5578
}
56-
57-
@Override
58-
public void setDamage(ItemStack stack, int damage) {
59-
60-
}
61-
62-
@Override
63-
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltip, TooltipFlag flagIn) {
64-
super.appendHoverText(stack, context, tooltip, flagIn);
65-
tooltip.add(ElectroTextUtils.tooltip("railguntemp", ChatFormatter.getChatDisplayShort(IItemTemperate.getTemperature(stack), DisplayUnits.TEMPERATURE_CELCIUS)).withStyle(ChatFormatting.YELLOW));
66-
tooltip.add(ElectroTextUtils.tooltip("railgunmaxtemp", ChatFormatter.getChatDisplayShort(overheatTemperature, DisplayUnits.TEMPERATURE_CELCIUS)).withStyle(ChatFormatting.YELLOW));
67-
if (IItemTemperate.getTemperature(stack) >= getOverheatTemp()) {
68-
tooltip.add(ElectroTextUtils.tooltip("railgunoverheat").withStyle(ChatFormatting.RED, ChatFormatting.BOLD));
69-
}
70-
FluidStack fluid = stack.getOrDefault(VoltaicDataComponentTypes.FLUID_STACK.get(), FluidStackComponent.EMPTY).fluid;
71-
tooltip.add(VoltaicTextUtils.ratio(ChatFormatter.formatFluidMilibuckets(fluid.getAmount()), ChatFormatter.formatFluidMilibuckets(CAPACITY)).withStyle(ChatFormatting.GRAY));
79+
FluidStack fluid = stack.getOrDefault(VoltaicDataComponentTypes.FLUID_STACK.get(),
80+
FluidStackComponent.EMPTY).fluid;
81+
tooltip.add(VoltaicTextUtils.ratio(ChatFormatter.formatFluidMilibuckets(fluid.getAmount()),
82+
ChatFormatter.formatFluidMilibuckets(CAPACITY)).withStyle(ChatFormatting.GRAY));
83+
}
84+
85+
@Override
86+
public void addCreativeModeItems(CreativeModeTab group, List<ItemStack> items) {
87+
List<ItemStack> superItems = new ArrayList<>();
88+
super.addCreativeModeItems(group, superItems);
89+
for (ItemStack stack : superItems) {
90+
if (stack.getItem() instanceof ItemRailgun) {
91+
IItemTemperate.setTemperature(stack, 0);
92+
}
7293
}
73-
74-
@Override
75-
public void addCreativeModeItems(CreativeModeTab group, List<ItemStack> items) {
76-
List<ItemStack> superItems = new ArrayList<>();
77-
super.addCreativeModeItems(group, superItems);
78-
for (ItemStack stack : superItems) {
79-
if (stack.getItem() instanceof ItemRailgun) {
80-
IItemTemperate.setTemperature(stack, 0);
81-
}
94+
items.addAll(superItems);
95+
}
96+
97+
@Override
98+
public void inventoryTick(ItemStack stack, Level worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
99+
super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
100+
((ItemRailgun) stack.getItem()).loseHeat(stack, tempPerTick, 0.0, false);
101+
}
102+
103+
@Override
104+
public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
105+
return !oldStack.is(newStack.getItem());
106+
}
107+
108+
public double getMaxTemp() {
109+
return overheatTemperature;
110+
}
111+
112+
public double getOverheatTemp() {
113+
return overheatTemperature * tempThreshold;
114+
}
115+
116+
@Override
117+
public TemperateItemProperties getTemperteProperties() {
118+
return temperateProperties;
119+
}
120+
121+
public static Predicate<FluidStack> getPredicate() {
122+
return fluid -> fluid.getFluid().builtInRegistryHolder().is(VoltaicTags.Fluids.AMMONIA);
123+
}
124+
125+
@EventBusSubscriber(value = Dist.CLIENT, modid = Electrodynamics.ID, bus = EventBusSubscriber.Bus.MOD)
126+
private static class ColorHandler {
127+
128+
@SubscribeEvent
129+
public static void registerColoredBlocks(RegisterColorHandlersEvent.Item event) {
130+
ITEMS.forEach(item -> event.register((stack, index) -> {
131+
if (index != 1) {
132+
return Color.WHITE.color();
82133
}
83-
items.addAll(superItems);
84-
}
85-
86-
@Override
87-
public void inventoryTick(ItemStack stack, Level worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
88-
super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
89-
((ItemRailgun) stack.getItem()).loseHeat(stack, tempPerTick, 0.0, false);
90-
}
91-
92-
@Override
93-
public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
94-
return !oldStack.is(newStack.getItem());
95-
}
96134

97-
public double getMaxTemp() {
98-
return overheatTemperature;
99-
}
135+
double currHeat = IItemTemperate.getTemperature(stack);
100136

101-
public double getOverheatTemp() {
102-
return overheatTemperature * tempThreshold;
103-
}
104-
105-
@Override
106-
public TemperateItemProperties getTemperteProperties() {
107-
return temperateProperties;
108-
}
109-
110-
public static Predicate<FluidStack> getPredicate() {
111-
return fluid -> fluid.getFluid().builtInRegistryHolder().is(VoltaicTags.Fluids.AMMONIA);
112-
}
113-
114-
@EventBusSubscriber(value = Dist.CLIENT, modid = Electrodynamics.ID, bus = EventBusSubscriber.Bus.MOD)
115-
private static class ColorHandler {
116-
117-
@SubscribeEvent
118-
public static void registerColoredBlocks(RegisterColorHandlersEvent.Item event) {
119-
ITEMS.forEach(item -> event.register((stack, index) -> {
120-
if (index != 1) {
121-
return Color.WHITE.color();
122-
}
123-
124-
double currHeat = IItemTemperate.getTemperature(stack);
125-
126-
if(currHeat <= 0){
127-
return HEAT_COLORS[0].color();
128-
}
129-
130-
double maxHeat = item.getMaxTemp();
131-
132-
double amtPerTier = maxHeat / HEAT_COLORS.length;
137+
if (currHeat <= 0) {
138+
return HEAT_COLORS[0].color();
139+
}
133140

134-
int threshhold = (int) (currHeat / amtPerTier);
141+
double maxHeat = item.getMaxTemp();
135142

136-
if(threshhold == HEAT_COLORS.length - 1){
137-
return HEAT_COLORS[threshhold].color();
138-
}
143+
double amtPerTier = maxHeat / HEAT_COLORS.length;
139144

140-
double amtNextTier = (currHeat - amtPerTier * threshhold) / amtPerTier;
145+
int threshhold = (int) (currHeat / amtPerTier);
141146

142-
return HEAT_COLORS[threshhold].blend(HEAT_COLORS[threshhold + 1], amtNextTier).color();
147+
if (threshhold >= HEAT_COLORS.length - 1) {
148+
return HEAT_COLORS[threshhold].color();
149+
}
143150

151+
double amtNextTier = (currHeat - amtPerTier * threshhold) / amtPerTier;
144152

145-
}, item));
146-
}
153+
return HEAT_COLORS[threshhold].blend(HEAT_COLORS[threshhold + 1], amtNextTier).color();
147154

155+
}, item));
148156
}
149157

158+
}
159+
150160
}

0 commit comments

Comments
 (0)