aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/EventRegistry.java
blob: 238d7e720e97058ebc9d2d9d052cf0971b067189 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
package de.torui.coflsky;

import java.time.LocalDateTime;
import com.mojang.realmsclient.util.Pair;

import de.torui.coflsky.FlipHandler.Flip;
import de.torui.coflsky.commands.Command;
import de.torui.coflsky.commands.CommandType;
import de.torui.coflsky.commands.JsonStringCommand;
import de.torui.coflsky.commands.models.AuctionData;
import de.torui.coflsky.network.WSClient;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent.KeyInputEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent.MouseInputEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent.ClientDisconnectionFromServerEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.common.gameevent.TickEvent;

public class EventRegistry {

	@SubscribeEvent
	public void onDisconnectedFromServerEvent(ClientDisconnectionFromServerEvent event) {
		if(CoflSky.Wrapper.isRunning) {
			System.out.println("Disconnected from server");
			CoflSky.Wrapper.stop();
			System.out.println("CoflSky stopped");
		}
	}
	
	public long LastClick = System.currentTimeMillis();

	@SideOnly(Side.CLIENT)
	@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
	public void onMouseEvent(MouseInputEvent event) {
		onEvent(null);
	}

	@SideOnly(Side.CLIENT)
	@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
	public void onEvent(KeyInputEvent event) {

		if (CoflSky.keyBindings[0].isPressed()) {
			if (WSCommandHandler.lastOnClickEvent != null) {

				String command = WSCommandHandler.lastOnClickEvent;
				WSCommandHandler.lastOnClickEvent = null;
				WSCommandHandler.HandleCommand(
						new JsonStringCommand(CommandType.Execute, WSClient.gson.toJson(command)),
						Minecraft.getMinecraft().thePlayer);
			}

		}
		if(CoflSky.keyBindings[1].isKeyDown()) {
			if((System.currentTimeMillis() - LastClick) >= 400) {
						
				Flip f = WSCommandHandler.flipHandler.fds.GetHighestFlip();
				
				if(f != null) {
					WSCommandHandler.Execute("/viewauction " + f.id, null);
					LastClick = System.currentTimeMillis();		
					String command =  WSClient.gson.toJson("/viewauction " + f.id);
					WSCommandHandler.flipHandler.fds.InvalidateFlip(f);
					
					WSCommandHandler.Execute("/cofl track besthotkey " + f.id, Minecraft.getMinecraft().thePlayer);
					CoflSky.Wrapper.SendMessage(new JsonStringCommand(CommandType.Clicked, command));		
				} else {
					WSCommandHandler.Execute("/cofl dialog nobestflip", Minecraft.getMinecraft().thePlayer);
				}			
				
			}
		}

	}

	@SideOnly(Side.CLIENT)
	//@SubscribeEvent
	public void DrawOntoGUI(RenderGameOverlayEvent rgoe) {

		if (rgoe.type == ElementType.CROSSHAIRS) {
			Minecraft mc = Minecraft.getMinecraft();
			mc.ingameGUI.drawString(Minecraft.getMinecraft().fontRendererObj, "Flips in Pipeline:" + WSCommandHandler.flipHandler.fds.CurrentFlips(), 0, 0, Integer.MAX_VALUE);
		}
	}

	public static String ExtractUuidFromInventory(IInventory inventory) {

		ItemStack stack = inventory.getStackInSlot(13);
		if (stack != null) {
			try {
				String uuid = stack.serializeNBT().getCompoundTag("tag").getCompoundTag("ExtraAttributes")
						.getString("uuid");
				if (uuid.length() == 0) {
					throw new Exception();
				}
				System.out.println("Item has the UUID: " + uuid);
				return uuid;
			} catch (Exception e) {
				System.out.println("Clicked item " + stack.getDisplayName() + " has the following meta: "
						+ stack.serializeNBT().toString());
			}
		}
		return "";
	}

	public static ItemStack GOLD_NUGGET = new ItemStack(
			Item.itemRegistry.getObject(new ResourceLocation("minecraft:gold_nugget")));

	public static final Pair<String, Pair<String, LocalDateTime>> EMPTY = Pair.of(null, Pair.of("",LocalDateTime.MIN));
	public static Pair<String, Pair<String, LocalDateTime>> last = EMPTY;
	
	@SubscribeEvent
	public void HandleChatEvent(ClientChatReceivedEvent sce) {
		if(CoflSky.Wrapper.isRunning && last.first() != null) {
			if(sce.message.getUnformattedText().startsWith("You claimed ")) {
				
				AuctionData ad = new AuctionData();
				ad.setItemId(last.second().first());
				
				if((LastViewAuctionInvocation+60*1000) >=  System.currentTimeMillis()) {
					ad.setAuctionId(LastViewAuctionUUID);
				} else {
					ad.setAuctionId("");
				}
				
				Command<AuctionData> data = new Command<>(CommandType.PurchaseConfirm, ad);
				CoflSky.Wrapper.SendMessage(data);
				System.out.println("PurchaseConfirm");
				last = EMPTY;
			}
			else if(last.second().second().plusSeconds(10).isBefore(LocalDateTime.now())) {
				last = EMPTY;
			}
		}
		
	}
	
	public static long lastStartTime = Long.MIN_VALUE;
	
	public static long LastViewAuctionInvocation = Long.MIN_VALUE;
	public static String LastViewAuctionUUID =null;
		
	@SideOnly(Side.CLIENT)
	@SubscribeEvent
	public void OnGuiClick(GuiScreenEvent.MouseInputEvent mie) {
		if (CoflSky.Wrapper.isRunning) {
			if (mie.gui instanceof GuiChest) { // verify that it's really a chest

				ContainerChest chest = (ContainerChest) ((GuiChest) mie.gui).inventorySlots;

				IInventory inv = chest.getLowerChestInventory();
				if (inv.hasCustomName()) { // verify that the chest actually has a custom name
					String chestName = inv.getName();

					if (chestName.equalsIgnoreCase("BIN Auction View") || chestName.equalsIgnoreCase("Ekwav")) {
					
						ItemStack heldItem = Minecraft.getMinecraft().thePlayer.inventory.getItemStack();
						
						if (heldItem != null) {
							System.out.println("Clicked on: " + heldItem.getItem().getRegistryName());
							
							String itemUUID = ExtractUuidFromInventory(inv);
							
							if(System.currentTimeMillis() > lastStartTime) {
								
								if (heldItem.isItemEqual(GOLD_NUGGET)) {
									AuctionData ad = new AuctionData();
									ad.setItemId(itemUUID);
														
									if((LastViewAuctionInvocation+60*1000) >=  System.currentTimeMillis()) {
										ad.setAuctionId(LastViewAuctionUUID);
									} else {
										ad.setAuctionId("");
									}
									
									Command<AuctionData> data = new Command<>(CommandType.PurchaseStart, ad);
									CoflSky.Wrapper.SendMessage(data);
									System.out.println("PurchaseStart");
									last = Pair.of("You claimed ", Pair.of(itemUUID, LocalDateTime.now()));
									lastStartTime = System.currentTimeMillis() + 200 /*ensure a small debounce*/;
								} 
							}
						
						}

					}
				}
			}

		}

	}

	@SubscribeEvent
    public void OnRenderTick(TickEvent.RenderTickEvent event) {
		CountdownTimer.onRenderTick(event);
	}
}