aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/WSCommandHandler.java
blob: bbc3021bb68a0a095dbbd2be29db495fd362b8b7 (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
package de.torui.coflsky;

import de.torui.coflsky.core.Command;
import de.torui.coflsky.core.SoundCommand;
import de.torui.coflsky.core.WriteToChatCommand;
import de.torui.coflsky.websocket.WSClient;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.ISound;
import net.minecraft.client.audio.PositionedSound;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.audio.SoundCategory;
import net.minecraft.client.audio.SoundEventAccessorComposite;
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.client.audio.SoundManager;
import net.minecraft.command.ICommandManager;
import net.minecraft.entity.Entity;
import net.minecraft.event.ClickEvent;
import net.minecraft.event.ClickEvent.Action;
import net.minecraft.event.HoverEvent;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.ResourceLocation;

public class WSCommandHandler {

	public static String lastOnClickEvent;

	public static boolean HandleCommand(Command cmd, Entity sender) {
		// Entity sender = Minecraft.getMinecraft().thePlayer;
		System.out.println("Handling Command=" + cmd.toString());
		switch (cmd.getType()) {
		case WriteToChat:
			WriteToChat(cmd);
			break;
		case Execute:
			Execute(cmd, sender);
			break;
		case PlaySound:
			PlaySound(cmd, sender);
			break;
		case ChatMessage:
			ChatMessage(cmd);
			break;
		default:
			break;
		}

		return true;
	}

	private static void PlaySound(Command cmd, Entity sender) {

		SoundCommand sc = WSClient.gson.fromJson(cmd.getData(), SoundCommand.class);
		
		SoundHandler handler = Minecraft.getMinecraft().getSoundHandler();

		// random.explode
		PositionedSoundRecord psr = PositionedSoundRecord
				.create(new ResourceLocation(sc.Name), sc.Pitch);
		
		handler.playSound(psr);
	}

	private static void Execute(Command cmd, Entity sender) {
		System.out.println("Execute: " + cmd.getData() + " sender:" + sender);

		Minecraft.getMinecraft().thePlayer.sendChatMessage(WSClient.gson.fromJson(cmd.getData(), String.class));
	}

	
	private static IChatComponent CommandToChatComponent(WriteToChatCommand wcmd) {
		if (wcmd.Text != null) {
			IChatComponent comp = new ChatComponentText(wcmd.Text);

			ChatStyle style;
			if (wcmd.OnClick != null) {
				if (wcmd.OnClick.startsWith("http")) {
					style = new ChatStyle().setChatClickEvent(new ClickEvent(Action.OPEN_URL, wcmd.OnClick));
				} else {
					style = new ChatStyle()
							.setChatClickEvent(new ClickEvent(Action.RUN_COMMAND, "/cofl callback " + wcmd.OnClick));
				}
				comp.setChatStyle(style);
			}

			if (wcmd.Hover != null && !wcmd.Hover.isEmpty()) {
				if (comp.getChatStyle() == null)
					comp.setChatStyle(new ChatStyle());
				comp.getChatStyle().setChatHoverEvent(
						new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(wcmd.Hover)));
			}
			return comp;
		}
		return null;
	}
	
	private static void ChatMessage(Command cmd) {
		WriteToChatCommand[] list = WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand[].class);

		IChatComponent master = new ChatComponentText("");

		for (WriteToChatCommand wcmd : list) {
			IChatComponent comp = CommandToChatComponent(wcmd);
			if (comp != null)
				master.appendSibling(comp);
		}
		Minecraft.getMinecraft().thePlayer.addChatMessage(master);
	}

	

	private static void WriteToChat(Command cmd) {
		WriteToChatCommand wcmd = WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand.class);

		IChatComponent comp = CommandToChatComponent(wcmd);
		if (comp != null)
		{
			if(wcmd.OnClick != null)
				lastOnClickEvent = "/cofl callback " + wcmd.OnClick;
			Minecraft.getMinecraft().thePlayer.addChatMessage(comp);
		}
			
	}

}