Skip to content

Commit 61fe5f2

Browse files
authored
Merge pull request #60 from KJP12/pr/datatracker-patches
New Config: Allowed Health Tags
2 parents 9cd1cca + 9e94991 commit 61fe5f2

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

src/main/java/org/samo_lego/golfiv/mixin/packets/EntityTrackerUpdateS2CPacketMixin_DataPatch.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import net.minecraft.entity.ItemEntity;
55
import net.minecraft.entity.LivingEntity;
66
import net.minecraft.entity.Saddleable;
7-
import net.minecraft.entity.boss.WitherEntity;
87
import net.minecraft.entity.data.DataTracker;
98
import net.minecraft.entity.data.TrackedData;
10-
import net.minecraft.entity.passive.IronGolemEntity;
119
import net.minecraft.item.ItemStack;
1210
import net.minecraft.network.packet.s2c.play.EntityTrackerUpdateS2CPacket;
1311
import net.minecraft.util.math.MathHelper;
@@ -53,26 +51,28 @@ public class EntityTrackerUpdateS2CPacketMixin_DataPatch {
5351

5452
Entity entity = ((DataTrackerAccessor) tracker).getTrackedEntity();
5553

56-
if (golfConfig.packet.removeHealthTags && entity instanceof LivingEntity && entity.isAlive() && !(entity instanceof Saddleable)) {
54+
if (golfConfig.packet.removeHealthTags && entity instanceof LivingEntity livingEntity && entity.isAlive() && !(entity instanceof Saddleable)) {
5755
trackedValues.removeIf(trackedValue -> trackedValue.getData() == LIVING_ENTITY_HEALTH);
5856
trackedValues.removeIf(trackedValue -> trackedValue.getData() == PLAYER_ENTITY_ABSORPTION);
5957

60-
if (entity instanceof IronGolemEntity || entity instanceof WitherEntity) {
61-
// Reinjects the health data aligned to quarters.
62-
LivingEntity livingEntity = (LivingEntity) entity;
58+
// This allows for iron golems to be visually broken, withers to have their shields,
59+
// and wolves to show their health, while still spoofing the health to a variable degree.
60+
// This is editable in GolfConfig as allowHealthTags.
61+
if (golfConfig.packet.allowedHealthTags.containsKey(entity.getType())) {
62+
float percentage = golfConfig.packet.allowedHealthTags.getFloat(entity.getType());
63+
float divider = livingEntity.getMaxHealth() * percentage;
6364

64-
// This takes away 1, divides by 25, floors, multiplies and add 1,
65-
// spoofing health to be within 25 of the actual value.
66-
// This allows for the iron golem to be visually broken, and for the wither to have its shield.
67-
Float newHealth = MathHelper.floor((livingEntity.getHealth() - 1F) / 25F) * 25F + 1F;
65+
// Shortcuts to livingEntity.getHealth on <= 1F.
66+
Float newHealth = divider <= 1F ? livingEntity.getHealth() :
67+
MathHelper.floor((livingEntity.getHealth() - 1F) / divider) * divider + 1F;
6868

6969
DataTracker.Entry<Float> fakeEntry = new DataTracker.Entry<>(LIVING_ENTITY_HEALTH, newHealth);
7070
trackedValues.add(fakeEntry);
7171
}
72-
} else if (golfConfig.packet.removeDroppedItemInfo && entity instanceof ItemEntity) {
72+
} else if (golfConfig.packet.removeDroppedItemInfo && entity instanceof ItemEntity itemEntity) {
7373
boolean removed = trackedValues.removeIf(entry -> entry.getData() == ITEM_ENTITY_STACK); // Original item
7474
if (removed) {
75-
ItemStack original = ((ItemEntity) entity).getStack();
75+
ItemStack original = itemEntity.getStack();
7676

7777
DataTracker.Entry<ItemStack> fakeEntry = new DataTracker.Entry<>(ITEM_ENTITY_STACK, fakeStack(original, false));
7878
trackedValues.add(fakeEntry);

src/main/java/org/samo_lego/golfiv/storage/GolfConfig.java

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5+
import com.google.gson.TypeAdapter;
6+
import com.google.gson.annotations.JsonAdapter;
7+
import com.google.gson.stream.JsonReader;
8+
import com.google.gson.stream.JsonWriter;
9+
import it.unimi.dsi.fastutil.objects.Object2FloatMaps;
10+
import it.unimi.dsi.fastutil.objects.Object2FloatOpenHashMap;
11+
import net.minecraft.entity.EntityType;
512

6-
import java.io.BufferedReader;
7-
import java.io.File;
8-
import java.io.FileInputStream;
9-
import java.io.FileOutputStream;
10-
import java.io.IOException;
11-
import java.io.InputStreamReader;
12-
import java.io.OutputStreamWriter;
13-
import java.io.Writer;
13+
import java.io.*;
1414
import java.nio.charset.StandardCharsets;
1515
import java.util.ArrayList;
1616
import java.util.Arrays;
@@ -112,16 +112,33 @@ public static class Packet {
112112
/**
113113
* Removes entity health data from packets
114114
* sent to client.
115-
*
115+
* <p>
116116
* Status: working.
117117
*/
118118
public boolean removeHealthTags = true;
119119

120+
public final String _comment_allowedHealthTags_1 = "// Allows health tags for certain entities.";
121+
public final String _comment_allowedHealthTags_2 = "// This maps entity ID to percentage as decimal.";
122+
123+
/**
124+
* Entities that must have health sent to render correctly.
125+
* <p>
126+
* K -> Entities to allow health of.
127+
* V -> Increments by percentage of health to allow.
128+
* <p>
129+
* Implied by default is 1F, or alive and dead.
130+
*/
131+
@JsonAdapter(UnnecessaryEntityTypeMapAdapter.class)
132+
public Object2FloatOpenHashMap<EntityType<?>> allowedHealthTags = new Object2FloatOpenHashMap<>(
133+
new EntityType<?>[]{EntityType.WOLF, EntityType.WITHER, EntityType.IRON_GOLEM},
134+
new float[]{0F, 0.5F, 0.25F}
135+
);
136+
120137
/**
121138
* Removes entity equipment tags from
122139
* packets. Players will still see if item is enchanted,
123140
* but won't get the durability or stack size information.
124-
*
141+
* <p>
125142
* Status: working.
126143
*/
127144
public boolean removeEquipmentTags = true;
@@ -250,4 +267,39 @@ public void saveConfig(File configFile) {
250267
logError("Problem occurred when saving config: " + e.getMessage());
251268
}
252269
}
270+
271+
/**
272+
* Adapts {@link EntityType} between it and the identifier.
273+
* <p>
274+
* Unnecessary, as map-level shouldn't be needed to begin with,
275+
* yet arbitrary unforeseen restrictions require this anyways.
276+
*
277+
* @author KJP12
278+
*/
279+
private static final class UnnecessaryEntityTypeMapAdapter extends TypeAdapter<Object2FloatOpenHashMap<EntityType<?>>> {
280+
281+
@Override
282+
public void write(JsonWriter out, Object2FloatOpenHashMap<EntityType<?>> value) throws IOException {
283+
out.beginObject();
284+
var itr = Object2FloatMaps.fastIterator(value);
285+
while (itr.hasNext()) {
286+
var entry = itr.next();
287+
out.name(EntityType.getId(entry.getKey()).toString());
288+
out.value(entry.getFloatValue());
289+
}
290+
out.endObject();
291+
}
292+
293+
@Override
294+
public Object2FloatOpenHashMap<EntityType<?>> read(JsonReader in) throws IOException {
295+
in.beginObject();
296+
var map = new Object2FloatOpenHashMap<EntityType<?>>();
297+
while (in.hasNext()) {
298+
map.put(EntityType.get(in.nextName()).orElseThrow(() -> new IOException("Invalid entity type.")),
299+
(float) in.nextDouble());
300+
}
301+
in.endObject();
302+
return map;
303+
}
304+
}
253305
}

0 commit comments

Comments
 (0)