|
2 | 2 |
|
3 | 3 | import com.google.gson.Gson; |
4 | 4 | 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; |
5 | 12 |
|
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.*; |
14 | 14 | import java.nio.charset.StandardCharsets; |
15 | 15 | import java.util.ArrayList; |
16 | 16 | import java.util.Arrays; |
@@ -112,16 +112,33 @@ public static class Packet { |
112 | 112 | /** |
113 | 113 | * Removes entity health data from packets |
114 | 114 | * sent to client. |
115 | | - * |
| 115 | + * <p> |
116 | 116 | * Status: working. |
117 | 117 | */ |
118 | 118 | public boolean removeHealthTags = true; |
119 | 119 |
|
| 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 | + |
120 | 137 | /** |
121 | 138 | * Removes entity equipment tags from |
122 | 139 | * packets. Players will still see if item is enchanted, |
123 | 140 | * but won't get the durability or stack size information. |
124 | | - * |
| 141 | + * <p> |
125 | 142 | * Status: working. |
126 | 143 | */ |
127 | 144 | public boolean removeEquipmentTags = true; |
@@ -250,4 +267,39 @@ public void saveConfig(File configFile) { |
250 | 267 | logError("Problem occurred when saving config: " + e.getMessage()); |
251 | 268 | } |
252 | 269 | } |
| 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 | + } |
253 | 305 | } |
0 commit comments