aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/re/jag/parquet/commands/Savedata.java
blob: 20d6210713704d82bf3822121b5addd00b3c0450 (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
package re.jag.parquet.commands;

import java.util.UUID;

import static net.minecraft.server.command.CommandManager.literal;
import static net.minecraft.server.command.CommandManager.argument;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.authlib.GameProfile;

import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.command.arguments.EntityArgumentType;
import net.minecraft.command.arguments.ObjectiveArgumentType;
import net.minecraft.command.arguments.ObjectiveCriteriaArgumentType;
import net.minecraft.item.Item;
import net.minecraft.scoreboard.ScoreboardCriterion;
import net.minecraft.scoreboard.ScoreboardObjective;
import net.minecraft.scoreboard.ScoreboardPlayerScore;
import net.minecraft.scoreboard.ServerScoreboard;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.stat.Stat;
import net.minecraft.stat.StatType;
import net.minecraft.block.Block;

public class Savedata {
	public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
		 LiteralArgumentBuilder<ServerCommandSource> savedata = literal("savedata").
	                requires(source -> source.hasPermissionLevel(4)).
	                executes((c) -> help(c.getSource())).
	                then( literal("playersave").
                			then(literal("list").
                				executes((c) -> list_local_saves(c.getSource()))
                		).
                			then(argument("UUID", StringArgumentType.word()).
                					executes((c) -> get_player_save_uuid(c.getSource(), StringArgumentType.getString(c, "UUID")))
                		)
                	).
	                then(literal("stats").
	                		then(argument("player", EntityArgumentType.player()).
			                		then(argument("criteria", ObjectiveCriteriaArgumentType.objectiveCriteria()).
			                				executes((c) -> print_player_stat (
			                						c.getSource(),
			                						EntityArgumentType.getPlayer(c, "player"),
			                						ObjectiveCriteriaArgumentType.getCriteria(c, "criteria") 
			                				)).
			                				then(literal("import").then(argument("score", ObjectiveArgumentType.objective()).
			                						executes((c) -> import_stat_to_scoreboard (
			                								c.getSource(), 
			                								EntityArgumentType.getPlayer(c, "player"), 
			                								ObjectiveCriteriaArgumentType.getCriteria(c, "criteria"), 
			                								ObjectiveArgumentType.getWritableObjective(c, "score"), 1)).
			                						then(argument("multiplier", FloatArgumentType.floatArg()).
			                								executes((c) ->  import_stat_to_scoreboard(
			                										c.getSource(), 
			                										EntityArgumentType.getPlayer(c, "player"), 
			                										ObjectiveCriteriaArgumentType.getCriteria(c, "criteria"), 
			                										ObjectiveArgumentType.getWritableObjective(c, "score"), 
			                										FloatArgumentType.getFloat(c, "multiplier"))
			                								)
			                						)
			                				))
			                		)
	                				)
                	);
		 dispatcher.register(savedata);
	}
	
	private static int help(ServerCommandSource source) {
		//TODO Help Text
		source.sendFeedback(new LiteralText("Test"), false);
		return 1;
	}
	
	private static int list_local_saves(ServerCommandSource source) {
		String uuid_list[] = source.getMinecraftServer().getWorld(DimensionType.OVERWORLD).getSaveHandler().getSavedPlayerIds();
		
		for (int i = 0; i < uuid_list.length; i++) {
			GameProfile profile = source.getMinecraftServer().getUserCache().getByUuid(UUID.fromString(uuid_list[i]));
			String name = "N.A.";
			
			if(profile != null) 
				 name = profile.getName();
			
			source.sendFeedback(new LiteralText(name + " (" + uuid_list[i] + ")"), false);
		}
		
		return 1;
	}
	
	private static int import_stat_to_scoreboard(ServerCommandSource source, ServerPlayerEntity player,ScoreboardCriterion stat, ScoreboardObjective objective, float multiplier) {
		int score = get_player_stat(source, player, stat);
		if (score < 0)
			return 0;
		
		ServerScoreboard server_scoreboard = source.getMinecraftServer().getScoreboard();
		ScoreboardPlayerScore player_score = server_scoreboard.getPlayerScore(player.getEntityName(), objective);
		
		int mod_score = multiplier >= 0 ? (int)((float)score * multiplier) : (int)((float)score / multiplier * (-1));
		
		player_score.setScore(mod_score);
		
		return 1;
	}
	
	private static int get_player_save_uuid(ServerCommandSource source, String uuid) {
		return 1;
	}

	private static int print_player_stat(ServerCommandSource source, ServerPlayerEntity player, ScoreboardCriterion stat) {
		Integer stat_value = get_player_stat(source, player, stat);
		
		if (stat_value >= 0)
		{
			source.sendFeedback(new LiteralText(stat_value.toString()), false);
			return 1;
		}
		return 0;
	}
	
	private static int get_player_stat(ServerCommandSource source, ServerPlayerEntity player, ScoreboardCriterion criteria) {
		//TODO Add offline player stat read
		String criteria_name = criteria.getName();
		int criteria_name_seperator = criteria_name.indexOf(':');
		
		String registry_name="";
		String stat_name="";
		
		if (criteria_name_seperator < 0) {
			source.sendError(new LiteralText("Expected valid registry and stat ID"));
			return -1;
		} else {
			registry_name = criteria_name.substring(0,criteria_name_seperator);
			stat_name = criteria_name.substring(criteria_name_seperator + 1, criteria_name.length() );
		}
		
		StatType<?> stat_type = Registry.STAT_TYPE.get(Identifier.splitOn(registry_name, '.'));
		Object stat_obj = stat_type.getRegistry().get(Identifier.splitOn(stat_name, '.'));
		
		if (stat_obj instanceof Block) {
			@SuppressWarnings("unchecked")
			StatType<Block> stat_type_block = (StatType<Block>) stat_type;
			Stat<Block> stat = stat_type_block.getOrCreateStat((Block) stat_obj);
			return player.getStatHandler().getStat(stat);
		} else if (stat_obj instanceof Item) {
			@SuppressWarnings("unchecked")
			StatType<Item> stat_type_item = (StatType<Item>) stat_type;
			Stat<Item> stat = stat_type_item.getOrCreateStat((Item) stat_obj);
			return player.getStatHandler().getStat(stat);	
		} else if (stat_obj instanceof Identifier) {
			@SuppressWarnings("unchecked")
			StatType<Identifier> stat_type_ident = (StatType<Identifier>) stat_type;
			Stat<Identifier> stat = stat_type_ident.getOrCreateStat((Identifier) stat_obj);
			return player.getStatHandler().getStat(stat);
		} else {
			source.sendError(new LiteralText("Unknown Object"));
			return -1;
		}
	}
}