-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
EggFinder.java
198 lines (176 loc) · 12.7 KB
/
EggFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package de.hysky.skyblocker.skyblock.chocolatefactory;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.events.SkyblockEvents;
import de.hysky.skyblocker.utils.*;
import de.hysky.skyblocker.utils.scheduler.MessageScheduler;
import de.hysky.skyblocker.utils.waypoint.Waypoint;
import it.unimi.dsi.fastutil.objects.ObjectImmutableList;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.decoration.ArmorStandEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EggFinder {
private static final Pattern eggFoundPattern = Pattern.compile("^(?:HOPPITY'S HUNT You found a Chocolate|You have already collected this Chocolate) (Breakfast|Lunch|Dinner)");
private static final Pattern newEggPattern = Pattern.compile("^HOPPITY'S HUNT A Chocolate (Breakfast|Lunch|Dinner) Egg has appeared!$");
private static final Logger logger = LoggerFactory.getLogger("Skyblocker Egg Finder");
private static final LinkedList<ArmorStandEntity> armorStandQueue = new LinkedList<>();
private static final Location[] possibleLocations = {Location.CRIMSON_ISLE, Location.CRYSTAL_HOLLOWS, Location.DUNGEON_HUB, Location.DWARVEN_MINES, Location.HUB, Location.THE_END, Location.THE_PARK, Location.GOLD_MINE, Location.DEEP_CAVERNS, Location.SPIDERS_DEN, Location.THE_FARMING_ISLAND};
private static boolean isLocationCorrect = false;
private EggFinder() {
}
public static void init() {
ClientPlayConnectionEvents.JOIN.register((ignored, ignored2, ignored3) -> invalidateState());
SkyblockEvents.LOCATION_CHANGE.register(EggFinder::handleLocationChange);
ClientReceiveMessageEvents.GAME.register(EggFinder::onChatMessage);
WorldRenderEvents.AFTER_TRANSLUCENT.register(EggFinder::renderWaypoints);
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(ClientCommandManager.literal(SkyblockerMod.NAMESPACE)
.then(ClientCommandManager.literal("eggFinder")
.then(ClientCommandManager.literal("shareLocation")
.then(ClientCommandManager.argument("x", IntegerArgumentType.integer())
.then(ClientCommandManager.argument("y", IntegerArgumentType.integer())
.then(ClientCommandManager.argument("z", IntegerArgumentType.integer())
.then(ClientCommandManager.argument("eggType", StringArgumentType.word())
.executes(context -> {
MessageScheduler.INSTANCE.sendMessageAfterCooldown("[Skyblocker] Chocolate " + context.getArgument("eggType", String.class) + " Egg found at " + context.getArgument("x", Integer.class) + " " + context.getArgument("y", Integer.class) + " " + context.getArgument("z", Integer.class) + "!");
return Command.SINGLE_SUCCESS;
})))))))));
}
private static void handleLocationChange(Location location) {
for (Location possibleLocation : possibleLocations) {
if (location == possibleLocation) {
isLocationCorrect = true;
break;
}
}
if (!isLocationCorrect) {
armorStandQueue.clear();
return;
}
while (!armorStandQueue.isEmpty()) {
handleArmorStand(armorStandQueue.poll());
}
}
public static void checkIfEgg(Entity entity) {
if (entity instanceof ArmorStandEntity armorStand) checkIfEgg(armorStand);
}
public static void checkIfEgg(ArmorStandEntity armorStand) {
if (!SkyblockerConfigManager.get().helpers.chocolateFactory.enableEggFinder) return;
if (SkyblockTime.skyblockSeason.get() != SkyblockTime.Season.SPRING) return;
if (armorStand.hasCustomName() || !armorStand.isInvisible() || !armorStand.shouldHideBasePlate()) return;
if (Utils.getLocation() == Location.UNKNOWN) { //The location is unknown upon world change and will be changed via /locraw soon, so we can queue it for now
armorStandQueue.add(armorStand);
return;
}
if (isLocationCorrect) handleArmorStand(armorStand);
}
private static void handleArmorStand(ArmorStandEntity armorStand) {
for (ItemStack itemStack : armorStand.getArmorItems()) {
ItemUtils.getHeadTextureOptional(itemStack).ifPresent(texture -> {
for (EggType type : EggType.entries) { //Compare blockPos rather than entity to avoid incorrect matches when the entity just moves rather than a new one being spawned elsewhere
if (texture.equals(type.texture) && (type.egg == null || !type.egg.entity.getBlockPos().equals(armorStand.getBlockPos()))) {
handleFoundEgg(armorStand, type);
return;
}
}
});
}
}
private static void invalidateState() {
if (!SkyblockerConfigManager.get().helpers.chocolateFactory.enableEggFinder) return;
isLocationCorrect = false;
for (EggType type : EggType.entries) {
type.egg = null;
}
}
private static void handleFoundEgg(ArmorStandEntity entity, EggType eggType) {
eggType.egg = new Egg(entity, new Waypoint(entity.getBlockPos().up(2), SkyblockerConfigManager.get().helpers.chocolateFactory.waypointType, ColorUtils.getFloatComponents(eggType.color)));
if (!SkyblockerConfigManager.get().helpers.chocolateFactory.sendEggFoundMessages || System.currentTimeMillis() - eggType.messageLastSent < 1000) return;
eggType.messageLastSent = System.currentTimeMillis();
MinecraftClient.getInstance().player.sendMessage(
Constants.PREFIX.get()
.append("Found a ")
.append(Text.literal("Chocolate " + eggType + " Egg")
.withColor(eggType.color))
.append(" at " + entity.getBlockPos().up(2).toShortString() + "!")
.styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/skyblocker eggFinder shareLocation " + entity.getBlockX() + " " + (entity.getBlockY() + 2) + " " + entity.getBlockZ() + " " + eggType))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal("Click to share the location in chat!").formatted(Formatting.GREEN)))));
}
private static void renderWaypoints(WorldRenderContext context) {
if (!SkyblockerConfigManager.get().helpers.chocolateFactory.enableEggFinder) return;
for (EggType type : EggType.entries) {
Egg egg = type.egg;
if (egg != null && egg.waypoint.shouldRender()) egg.waypoint.render(context);
}
}
private static void onChatMessage(Text text, boolean overlay) {
if (overlay || !SkyblockerConfigManager.get().helpers.chocolateFactory.enableEggFinder) return;
Matcher matcher = eggFoundPattern.matcher(text.getString());
if (matcher.find()) {
try {
Egg egg = EggType.valueOf(matcher.group(1).toUpperCase()).egg;
if (egg != null) egg.waypoint.setFound();
} catch (IllegalArgumentException e) {
logger.error("[Skyblocker Egg Finder] Failed to find egg type for egg found message. Tried to match against: " + matcher.group(0), e);
}
}
matcher.usePattern(newEggPattern);
if (matcher.find()) {
try {
EggType.valueOf(matcher.group(1).toUpperCase()).egg = null;
} catch (IllegalArgumentException e) {
logger.error("[Skyblocker Egg Finder] Failed to find egg type for egg spawn message. Tried to match against: " + matcher.group(0), e);
}
}
}
record Egg(ArmorStandEntity entity, Waypoint waypoint) {}
@SuppressWarnings("DataFlowIssue") //Removes that pesky "unboxing of Integer might cause NPE" warning when we already know it's not null
enum EggType {
LUNCH(Formatting.BLUE.getColorValue(), "ewogICJ0aW1lc3R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ogICJ0ZXh0dXJlcyIgOiB7CiAgICAiU0tJTiIgOiB7CiAgICAgICJ1cmwiIDogImh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYTQ5MzMzZDg1YjhhMzE1ZDAzMzZlYjJkZjM3ZDhhNzE0Y2EyNGM1MWI4YzYwNzRmMWI1YjkyN2RlYjUxNmMyNCIKICAgIH0KICB9Cn0");
private Egg egg = null;
public final int color;
public final String texture;
/*
When a new egg spawns in the player's range, the order of packets/messages goes like this:
set_equipment -> new egg message -> set_entity_data
We have to set the egg to null to prevent the highlight from staying where it was before the new egg spawned,
and doing so causes the found message to get sent twice. This is the reason for the existence of this field, so that we can not send the 2nd message.
This doesn't fix the field being set twice, but that's not an issue anyway. It'd be much harder to fix the highlight issue mentioned above if it wasn't being set twice.
*/
private long messageLastSent = 0;
//This is to not create an array each time we iterate over the values
public static final ObjectImmutableList<EggType> entries = ObjectImmutableList.of(BREAKFAST, LUNCH, DINNER);
EggType(int color, String texture) {
this.color = color;
this.texture = texture;
}
@Override
public String toString() {
return switch (this) {
case LUNCH -> "Lunch";
case DINNER -> "Dinner";
case BREAKFAST -> "Breakfast";
};
}
}
}