Skip to content

Commit 06bfeb6

Browse files
committed
put Event Subscribers into seperate class
1 parent 58e6c5d commit 06bfeb6

File tree

2 files changed

+211
-173
lines changed

2 files changed

+211
-173
lines changed

src/client/java/com/coflnet/CoflModClient.java

Lines changed: 64 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import CoflCore.CoflCore;
44
import CoflCore.classes.*;
55
import CoflCore.CoflSkyCommand;
6-
import CoflCore.commands.CommandType;
7-
import CoflCore.commands.models.ChatMessageData;
86
import CoflCore.commands.models.FlipData;
9-
import CoflCore.commands.models.SoundData;
10-
import CoflCore.events.*;
117
import CoflCore.handlers.EventRegistry;
128
import com.coflnet.gui.RenderUtils;
139
import com.coflnet.gui.cofl.CoflBinGUI;
@@ -16,7 +12,6 @@
1612
import com.google.gson.GsonBuilder;
1713
import com.google.gson.JsonObject;
1814
import com.mojang.brigadier.arguments.StringArgumentType;
19-
import com.sun.java.accessibility.util.AWTEventMonitor;
2015
import net.fabricmc.api.ClientModInitializer;
2116
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
2217
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
@@ -26,68 +21,47 @@
2621
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
2722
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
2823
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
29-
import net.fabricmc.fabric.api.client.screen.v1.ScreenKeyboardEvents;
30-
import net.fabricmc.fabric.impl.client.event.lifecycle.ClientLifecycleEventsImpl;
3124
import net.fabricmc.loader.api.FabricLoader;
3225
import net.minecraft.client.MinecraftClient;
33-
import net.minecraft.client.font.TextRenderer;
3426
import net.minecraft.client.gui.screen.ChatScreen;
3527
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
3628
import net.minecraft.client.option.KeyBinding;
3729
import net.minecraft.client.util.InputUtil;
3830
import net.minecraft.entity.player.PlayerEntity;
31+
import net.minecraft.entity.player.PlayerInventory;
32+
import net.minecraft.inventory.Inventories;
33+
import net.minecraft.inventory.Inventory;
34+
import net.minecraft.item.ItemStack;
35+
import net.minecraft.item.Items;
36+
import net.minecraft.nbt.NbtCompound;
37+
import net.minecraft.nbt.NbtIo;
38+
import net.minecraft.registry.Registries;
3939
import net.minecraft.registry.entry.RegistryEntry;
40-
import net.minecraft.sound.SoundCategory;
4140
import net.minecraft.sound.SoundEvent;
4241
import net.minecraft.sound.SoundEvents;
43-
import net.minecraft.text.Text;
44-
import net.minecraft.world.timer.TimerCallbackSerializer;
45-
import org.greenrobot.eventbus.EventBus;
46-
import org.greenrobot.eventbus.Subscribe;
42+
import net.minecraft.util.collection.DefaultedList;
4743
import org.lwjgl.glfw.GLFW;
4844

49-
import javax.swing.*;
50-
import java.awt.*;
51-
import java.awt.event.KeyEvent;
52-
import java.awt.event.KeyListener;
53-
import java.lang.ref.Reference;
45+
import java.io.ByteArrayOutputStream;
46+
import java.io.IOException;
5447
import java.lang.reflect.Field;
5548
import java.nio.file.Path;
56-
import java.util.Arrays;
57-
import java.util.Timer;
58-
import java.util.TimerTask;
59-
60-
import static com.coflnet.Utils.ChatComponent;
49+
import java.util.*;
6150

6251
public class CoflModClient implements ClientModInitializer {
63-
private KeyBinding bestflipsKeyBinding;
64-
private boolean keyPressed = false;
65-
private int counter = 0;
52+
private static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
53+
private static boolean keyPressed = false;
54+
private static int counter = 0;
55+
public static KeyBinding bestflipsKeyBinding;
6656

6757
private String username = "";
68-
private static FlipData flipData = null;
69-
private static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
70-
private static Countdown countdownData = null;
71-
private static float countdown = 0.0f;
72-
private static boolean showCountdown = false;
73-
private static Timer timer = new Timer();
74-
private static TimerTask task = new TimerTask() {
75-
@Override
76-
public void run() {
77-
countdown -= 0.1f;
78-
if(countdown < 0.0) {
79-
showCountdown = false;
80-
timer.cancel();
81-
}
82-
}
83-
};
8458
@Override
8559
public void onInitializeClient() {
8660
username = MinecraftClient.getInstance().getSession().getUsername();
8761
Path configDir = FabricLoader.getInstance().getConfigDir();
8862
CoflCore cofl = new CoflCore();
8963
cofl.init(configDir);
90-
cofl.registerEventFile(this);
64+
cofl.registerEventFile(new EventSubscribers());
9165

9266
ClientLifecycleEvents.CLIENT_STARTED.register(mc -> {
9367
RenderUtils.init();
@@ -103,8 +77,6 @@ public void onInitializeClient() {
10377
ClientTickEvents.END_CLIENT_TICK.register(client -> {
10478
if(bestflipsKeyBinding.isPressed()) {
10579
if(counter == 0){
106-
// PlayerEntity player = MinecraftClient.getInstance().player;
107-
// player.getWorld().playSound(player, player.getBlockPos(), findByName("BLOCK_ANVIL_PLACE"), SoundCategory.MASTER, 1f, 1f);
10880
EventRegistry.onOpenBestFlip(username, true);
10981
}
11082
if(counter < 2) counter++;
@@ -113,7 +85,6 @@ public void onInitializeClient() {
11385
}
11486
});
11587

116-
11788
ClientPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
11889
if(MinecraftClient.getInstance() != null && MinecraftClient.getInstance().getCurrentServerEntry() != null && MinecraftClient.getInstance().getCurrentServerEntry().address.contains("hypixel.net")){
11990
System.out.println("Connected to Hypixel");
@@ -133,6 +104,7 @@ public void onInitializeClient() {
133104
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
134105
if (screen instanceof GenericContainerScreen gcs) {
135106
System.out.println(gcs.getTitle().getString());
107+
System.out.println("INV: "+gcs.getScreenHandler().getInventory().getClass().getName());
136108
if (CoflCore.config.purchaseOverlay != null && gcs.getTitle() != null
137109
&& (gcs.getTitle().getString().contains("BIN Auction View")
138110
&& gcs.getScreenHandler().getInventory().size() == 9 * 6
@@ -151,149 +123,31 @@ public void onInitializeClient() {
151123
});
152124

153125
HudRenderCallback.EVENT.register((drawContext, tickCounter) -> {
154-
if (showCountdown && countdownData != null
126+
if (EventSubscribers.showCountdown && EventSubscribers.countdownData != null
155127
&& (MinecraftClient.getInstance().currentScreen == null
156128
|| MinecraftClient.getInstance().currentScreen instanceof ChatScreen)){
157129
RenderUtils.drawStringWithShadow(
158-
drawContext, countdownData.getPrefix()+"New flips in: "+String.format("%.1f", countdown),
159-
MinecraftClient.getInstance().getWindow().getWidth()/countdownData.getWidthPercentage(),
160-
MinecraftClient.getInstance().getWindow().getHeight()/countdownData.getHeightPercentage(),
161-
0xFFFFFFFF, countdownData.getScale()
130+
drawContext, EventSubscribers.countdownData.getPrefix()+"New flips in: "+String.format("%.1f", EventSubscribers.countdown),
131+
MinecraftClient.getInstance().getWindow().getWidth()/EventSubscribers.countdownData.getWidthPercentage(),
132+
MinecraftClient.getInstance().getWindow().getHeight()/EventSubscribers.countdownData.getHeightPercentage(),
133+
0xFFFFFFFF, EventSubscribers.countdownData.getScale()
162134
);
163135
}
164136
});
165137
}
166138

167139
public static FlipData popFlipData(){
168-
FlipData fd = flipData;
169-
flipData = null;
140+
FlipData fd = EventSubscribers.flipData;
141+
EventSubscribers.flipData = null;
170142
return fd;
171143
}
172144

173-
@Subscribe
174-
public void WriteToChat(OnWriteToChatReceive command){
175-
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(ChatComponent(command.ChatMessage));
176-
}
177-
178-
@Subscribe
179-
public void onChatMessage(OnChatMessageReceive event){
180-
for (ChatMessage message : event.ChatMessages) {
181-
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(ChatComponent(message));
182-
}
183-
}
184-
185-
@Subscribe
186-
public void onModChatMessage(OnModChatMessage event){
187-
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(Text.of(event.message));
188-
}
189-
190-
@Subscribe
191-
public void onCountdownReceive(OnCountdownReceive event){
192-
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(Text.of("COUNTDOWN RECEIVED: "+event.CountdownData.getDuration()));
193-
countdown = event.CountdownData.getDuration();
194-
countdownData = event.CountdownData;
195-
showCountdown = true;
196-
197-
System.out.println("COUNTDOWNDATA:");
198-
System.out.println("w%: "+countdownData.getWidthPercentage());
199-
System.out.println("ww: "+MinecraftClient.getInstance().getWindow().getWidth());
200-
System.out.println("w% of ww: "+MinecraftClient.getInstance().getWindow().getWidth()/countdownData.getWidthPercentage());
201-
System.out.println("h%: "+countdownData.getHeightPercentage());
202-
System.out.println("wh: "+MinecraftClient.getInstance().getWindow().getHeight());
203-
System.out.println("h% of wh: "+MinecraftClient.getInstance().getWindow().getHeight()/countdownData.getHeightPercentage());
204-
System.out.println("prefix: "+countdownData.getPrefix());
205-
System.out.println("scale: "+countdownData.getScale());
206-
207-
timer = new Timer();
208-
timer.scheduleAtFixedRate(new TimerTask() {
209-
@Override
210-
public void run() {
211-
countdown -= 0.1f;
212-
if(countdown < 0.0) {
213-
showCountdown = false;
214-
timer.cancel();
215-
}
216-
}
217-
}, 1, 100);
218-
}
219-
220-
@Subscribe
221-
public void onOpenAuctionGUI(OnOpenAuctionGUI event){
222-
flipData = event.flip;
223-
MinecraftClient.getInstance().getNetworkHandler().sendChatMessage(event.openAuctionCommand);
224-
}
225-
226-
@Subscribe
227-
public void onFlipReceive(OnFlipReceive event){
228-
Flip f = event.FlipData;
229-
FlipData fd = new FlipData(
230-
Arrays.stream(f.getMessages())
231-
.map(cm -> new ChatMessageData(
232-
cm.getText(),
233-
cm.getOnClick(),
234-
cm.getHover())
235-
).toArray(ChatMessageData[]::new),
236-
f.getId(),
237-
f.getWorth(),
238-
new SoundData(
239-
f.getSound().getSoundName(),
240-
f.getSound().getSoundPitch() == null ? 0 : f.getSound().getSoundPitch()
241-
),
242-
f.getRender()
243-
);
244-
245-
if (bestflipsKeyBinding.isPressed()) {
246-
EventBus.getDefault().post(new OnOpenAuctionGUI("/viewauction "+fd.Id, fd));
247-
return;
248-
}
249-
250-
EventBus.getDefault().post(new OnChatMessageReceive(f.getMessages()));
251-
EventBus.getDefault().post(new OnPlaySoundReceive(f.getSound()));
252-
CoflCore.flipHandler.fds.Insert(fd);
253-
}
254-
255-
@Subscribe
256-
public void onReceiveCommand(ReceiveCommand event){
257-
if (event.command.getType() == CommandType.Flip){
258-
JsonObject jo = gson.fromJson(event.command.getData(), JsonObject.class);
259-
EventBus.getDefault().post(new OnFlipReceive(jsonObjToFlip(jo)));
260-
}
261-
}
262-
263-
@Subscribe
264-
public void onPlaySoundReceive(OnPlaySoundReceive event){
265-
if(event.Sound == null || event.Sound.getSoundName() == null) return;
266-
267-
String soundName = "";
268-
switch (event.Sound.getSoundName()){
269-
case "note.bass" -> soundName = "BLOCK_NOTE_BLOCK_BASS";
270-
case "note.pling" -> soundName = "BLOCK_NOTE_BLOCK_PLING";
271-
case "note.hat" -> soundName = "BLOCK_NOTE_BLOCK_HAT";
272-
case "random.orb" -> soundName = "ENTITY_EXPERIENCE_ORB_PICKUP";
273-
default -> soundName = "";
274-
}
275-
276-
PlayerEntity player = MinecraftClient.getInstance().player;
277-
player.getWorld().playSound(
278-
player, player.getBlockPos(),
279-
findByName(soundName),
280-
SoundCategory.MASTER, 1f,
281-
event.Sound.getSoundPitch() == null ? 1f : (float) event.Sound.getSoundPitch()
282-
);
283-
}
284-
285-
@Subscribe
286-
public void onExecuteCommand(OnExecuteCommand event){
287-
System.out.println("ON EXEC COMMAND:"+event.Error);
288-
}
289-
290145
public static SoundEvent findByName(String name) {
291146
SoundEvent result = SoundEvents.BLOCK_NOTE_BLOCK_BELL.value();
292147

293148
for (Field f : SoundEvents.class.getDeclaredFields()) {
294149
if (f.getName().equalsIgnoreCase(name)) {
295150
try {
296-
SoundEvents.BLOCK_NOTE_BLOCK_PLING.value();
297151
try {
298152
result = (SoundEvent) f.get(SoundEvent.class);
299153
} catch (ClassCastException e){
@@ -308,7 +162,8 @@ public static SoundEvent findByName(String name) {
308162
return result;
309163
}
310164

311-
public static Flip jsonObjToFlip(JsonObject jsonObj){
165+
public static Flip jsonToFlip(String json){
166+
JsonObject jsonObj = gson.fromJson(json, JsonObject.class);
312167
JsonObject[] chatMessagesObj = gson.fromJson(jsonObj.get("messages"), JsonObject[].class);
313168
ChatMessage[] chatMessages = Arrays.stream(chatMessagesObj).map(jsonObject -> new ChatMessage(
314169
jsonObject.get("text").getAsString(),
@@ -324,4 +179,40 @@ public static Flip jsonObjToFlip(JsonObject jsonObj){
324179
String target = gson.fromJson(jsonObj.get("target"), String.class);
325180
return new Flip(chatMessages, id, worth, sound, auction, render, target);
326181
}
327-
}
182+
183+
public static String inventoryToNBT(Inventory inventory){
184+
NbtCompound nbtCompound = new NbtCompound();
185+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
186+
PlayerEntity player = MinecraftClient.getInstance().player;
187+
DefaultedList<ItemStack> itemStacks = DefaultedList.of();
188+
189+
for (int i = 0; i < inventory.size(); i++) {
190+
itemStacks.add(inventory.getStack(i));
191+
}
192+
193+
try {
194+
Inventories.writeNbt(nbtCompound, itemStacks, player.getRegistryManager());
195+
nbtCompound.put("i", nbtCompound.get("Items"));
196+
nbtCompound.remove("Items");
197+
198+
System.out.println(nbtCompound.get("i").asString());
199+
200+
NbtIo.writeCompressed(nbtCompound, baos);
201+
return Base64.getEncoder().encodeToString(baos.toByteArray());
202+
203+
} catch (IOException e){}
204+
return "";
205+
}
206+
207+
public static String[] getItemIdsFromInventory(PlayerInventory inventory){
208+
ArrayList<String> res = new ArrayList<>();
209+
210+
for (int i = 0; i < inventory.size(); i++) {
211+
ItemStack stack = inventory.getStack(i);
212+
if (stack.getItem() != Items.AIR) res.add(Registries.ITEM.getId(stack.getItem()).toString());
213+
}
214+
215+
return res.toArray(String[]::new);
216+
}
217+
}
218+

0 commit comments

Comments
 (0)