|
8 | 8 | import io.ix0rai.rainglow.config.RainglowConfig; |
9 | 9 | import io.ix0rai.rainglow.data.*; |
10 | 10 | import net.fabricmc.api.ModInitializer; |
| 11 | +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; |
11 | 12 | import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; |
12 | 13 | import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; |
13 | 14 | import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; |
@@ -71,8 +72,14 @@ public void onInitialize() { |
71 | 72 | }); |
72 | 73 |
|
73 | 74 | ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { |
74 | | - COLOURS.clear(); |
| 75 | + // Only clear colours on disconnect if server is NOT single-player to prevent NBT save failure (Unsure how this works with Lan-instances) |
| 76 | + if (!server.isSingleplayer()) { |
| 77 | + COLOURS.clear(); |
| 78 | + } |
75 | 79 | }); |
| 80 | + |
| 81 | + // Instead use SERVER_STOPPED for clearing colours from single-player worlds. (Doesn't affect others because mod would most likely be shutdown in non-single-player instances) |
| 82 | + ServerLifecycleEvents.SERVER_STOPPED.register(server -> COLOURS.clear()); |
76 | 83 | } |
77 | 84 |
|
78 | 85 | public static Identifier id(String id) { |
@@ -102,15 +109,14 @@ public static Text translatableText(String key) { |
102 | 109 | return Text.translatable(translatableTextKey(key)); |
103 | 110 | } |
104 | 111 |
|
105 | | - public static RainglowColour getColour(Entity entity) { |
106 | | - RainglowColour colour = COLOURS.get(entity.getUuid()); |
107 | | - RainglowEntity entityType = RainglowEntity.get(entity); |
| 112 | + public static RainglowColour getColour(UUID uuid, World world, RainglowEntity type) { |
| 113 | + RainglowColour colour = COLOURS.get(uuid); |
108 | 114 |
|
109 | 115 | // generate random colour if the squid's colour isn't currently loaded |
110 | | - if (colourUnloaded(entity.getWorld(), entityType, colour)) { |
| 116 | + if (colourUnloaded(world, type, colour)) { |
111 | 117 | // Use last generated colour if not null else generate a new colour |
112 | | - colour = generateRandomColour(entity.getWorld(), entity.getRandom()); |
113 | | - COLOURS.put(entity.getUuid(), colour); |
| 118 | + colour = generateRandomColour(world, world.getRandom()); |
| 119 | + COLOURS.put(uuid, colour); |
114 | 120 | } |
115 | 121 |
|
116 | 122 | return colour; |
|
0 commit comments