forked from virt-mirrors/Sit
move utils to Utl.HandSettings
This commit is contained in:
parent
eaef4515a3
commit
28616a1974
4 changed files with 68 additions and 45 deletions
|
@ -23,7 +23,8 @@ import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.math.Box;
|
import net.minecraft.util.math.Box;
|
||||||
import net.minecraft.util.math.Vec3d;
|
import net.minecraft.util.math.Vec3d;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import one.oth3r.sit.Utl.HandType;
|
import one.oth3r.sit.Utl.HandSettings.HandType;
|
||||||
|
import one.oth3r.sit.file.Config;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@ -38,25 +39,25 @@ public class Events {
|
||||||
ArrayList<UseAction> notUsable = new ArrayList<>(food);
|
ArrayList<UseAction> notUsable = new ArrayList<>(food);
|
||||||
notUsable.add(UseAction.NONE);
|
notUsable.add(UseAction.NONE);
|
||||||
HashMap<HandType, ItemStack> itemMap = new HashMap<>();
|
HashMap<HandType, ItemStack> itemMap = new HashMap<>();
|
||||||
itemMap.put(HandType.main,player.getMainHandStack());
|
itemMap.put(Utl.HandSettings.HandType.main,player.getMainHandStack());
|
||||||
itemMap.put(HandType.off,player.getOffHandStack());
|
itemMap.put(Utl.HandSettings.HandType.off,player.getOffHandStack());
|
||||||
// if sneaking cant sit
|
// if sneaking cant sit
|
||||||
if (player.isSneaking()) return false;
|
if (player.isSneaking()) return false;
|
||||||
// for both hands
|
// for both hands
|
||||||
for (HandType type:HandType.values()) {
|
for (HandType type: Utl.HandSettings.HandType.values()) {
|
||||||
ItemStack targetStack = itemMap.get(type);
|
ItemStack targetStack = itemMap.get(type);
|
||||||
// if req is empty and the item isn't empty, false
|
// if req is empty and the item isn't empty, false
|
||||||
if (Utl.getReq(player,type).equals(config.HandRequirement.empty) && !targetStack.isEmpty()) return false;
|
if (Utl.HandSettings.getReq(player,type).equals(Config.HandRequirement.empty) && !targetStack.isEmpty()) return false;
|
||||||
// if req is restrictive
|
// if req is restrictive
|
||||||
if (Utl.getReq(player,type).equals(config.HandRequirement.restrictive)) {
|
if (Utl.HandSettings.getReq(player,type).equals(Config.HandRequirement.restrictive)) {
|
||||||
// if item is in blacklist, false
|
// if item is in blacklist, false
|
||||||
if (checkList(Utl.getList(player,type,"blacklist"),targetStack)) return false;
|
if (checkList(Utl.HandSettings.getList(player,type,"blacklist"),targetStack)) return false;
|
||||||
// if item is NOT in whitelist
|
// if item is NOT in whitelist
|
||||||
if (!checkList(Utl.getList(player,type,"whitelist"),targetStack)) {
|
if (!checkList(Utl.HandSettings.getList(player,type,"whitelist"),targetStack)) {
|
||||||
// if block is restricted and items is block, false, ect
|
// if block is restricted and items is block, false, ect
|
||||||
if (Utl.getBool(player,type,"block") && (targetStack.getItem() instanceof BlockItem)) return false;
|
if (Utl.HandSettings.getBool(player,type,"block") && (targetStack.getItem() instanceof BlockItem)) return false;
|
||||||
if (Utl.getBool(player,type,"food") && food.contains(targetStack.getUseAction())) return false;
|
if (Utl.HandSettings.getBool(player,type,"food") && food.contains(targetStack.getUseAction())) return false;
|
||||||
if (Utl.getBool(player,type,"usable") && !notUsable.contains(targetStack.getUseAction())) return false;
|
if (Utl.HandSettings.getBool(player,type,"usable") && !notUsable.contains(targetStack.getUseAction())) return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,7 +193,7 @@ public class Events {
|
||||||
ServerPlayerEntity player = handler.player;
|
ServerPlayerEntity player = handler.player;
|
||||||
checkPlayers.put(player,2);
|
checkPlayers.put(player,2);
|
||||||
// put server settings in the player settings
|
// put server settings in the player settings
|
||||||
Sit.playerSettings.put(player,Utl.getHandSettings());
|
Sit.playerSettings.put(player, Utl.HandSettings.getHandSettings());
|
||||||
});
|
});
|
||||||
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
|
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
|
||||||
ServerPlayerEntity player = handler.player;
|
ServerPlayerEntity player = handler.player;
|
||||||
|
|
|
@ -3,12 +3,9 @@ package one.oth3r.sit;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.util.ReferenceCountUtil;
|
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||||
import net.minecraft.network.PacketByteBuf;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.server.command.CommandManager;
|
import net.minecraft.server.command.CommandManager;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
|
|
@ -20,6 +20,6 @@ public class SitClient implements ClientModInitializer {
|
||||||
}
|
}
|
||||||
public static void sendPackets() {
|
public static void sendPackets() {
|
||||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||||
new PacketBuilder(gson.toJson(Utl.getHandSettings())).send();
|
new PacketBuilder(gson.toJson(Utl.HandSettings.getHandSettings())).send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import one.oth3r.sit.file.Config;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -11,35 +12,59 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Utl {
|
public class Utl {
|
||||||
enum HandType {
|
public static class HandSettings {
|
||||||
main,
|
public static HashMap<String,String> getHandSettings() {
|
||||||
off
|
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||||
|
HashMap<String,String> settings = new HashMap<>();
|
||||||
|
settings.put("hand.main.requirement",String.valueOf(Config.mainReq));
|
||||||
|
settings.put("hand.main.block",String.valueOf(Config.mainBlock));
|
||||||
|
settings.put("hand.main.food",String.valueOf(Config.mainFood));
|
||||||
|
settings.put("hand.main.usable",String.valueOf(Config.mainUsable));
|
||||||
|
settings.put("hand.main.whitelist",gson.toJson(Config.mainWhitelist));
|
||||||
|
settings.put("hand.main.blacklist",gson.toJson(Config.mainBlacklist));
|
||||||
|
settings.put("hand.off.requirement",String.valueOf(Config.offReq));
|
||||||
|
settings.put("hand.off.block",String.valueOf(Config.offBlock));
|
||||||
|
settings.put("hand.off.food",String.valueOf(Config.offFood));
|
||||||
|
settings.put("hand.off.usable",String.valueOf(Config.offUsable));
|
||||||
|
settings.put("hand.off.whitelist",gson.toJson(Config.offWhitelist));
|
||||||
|
settings.put("hand.off.blacklist",gson.toJson(Config.offBlacklist));
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Config.HandRequirement getReq(ServerPlayerEntity player, HandType type) {
|
||||||
|
return Config.HandRequirement.get(Sit.playerSettings.get(player).get("hand."+type+".requirement"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getList(ServerPlayerEntity player, HandType type, String setting) {
|
||||||
|
Type listType = new TypeToken<ArrayList<String>>() {}.getType();
|
||||||
|
return new Gson().fromJson(Sit.playerSettings.get(player).get("hand."+type+"."+setting),listType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean getBool(ServerPlayerEntity player, HandType type, String setting) {
|
||||||
|
return Boolean.parseBoolean(Sit.playerSettings.get(player).get("hand."+type+"."+setting));
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum HandType {
|
||||||
|
main,
|
||||||
|
off
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public static HashMap<String,String> getHandSettings() {
|
public static class Num {
|
||||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
public static boolean isInt(String string) {
|
||||||
HashMap<String,String> settings = new HashMap<>();
|
try {
|
||||||
settings.put("hand.main.requirement",String.valueOf(config.mainReq));
|
Integer.parseInt(string);
|
||||||
settings.put("hand.main.block",String.valueOf(config.mainBlock));
|
} catch (NumberFormatException nfe) {
|
||||||
settings.put("hand.main.food",String.valueOf(config.mainFood));
|
return false;
|
||||||
settings.put("hand.main.usable",String.valueOf(config.mainUsable));
|
}
|
||||||
settings.put("hand.main.whitelist",gson.toJson(config.mainWhitelist));
|
return true;
|
||||||
settings.put("hand.main.blacklist",gson.toJson(config.mainBlacklist));
|
}
|
||||||
settings.put("hand.off.requirement",String.valueOf(config.offReq));
|
public static boolean isFloat(String string) {
|
||||||
settings.put("hand.off.block",String.valueOf(config.offBlock));
|
try {
|
||||||
settings.put("hand.off.food",String.valueOf(config.offFood));
|
Float.parseFloat(string);
|
||||||
settings.put("hand.off.usable",String.valueOf(config.offUsable));
|
} catch (NumberFormatException nfe) {
|
||||||
settings.put("hand.off.whitelist",gson.toJson(config.offWhitelist));
|
return false;
|
||||||
settings.put("hand.off.blacklist",gson.toJson(config.offBlacklist));
|
}
|
||||||
return settings;
|
return true;
|
||||||
}
|
}
|
||||||
public static config.HandRequirement getReq(ServerPlayerEntity player, HandType type) {
|
|
||||||
return config.HandRequirement.get(Sit.playerSettings.get(player).get("hand."+type+".requirement"));
|
|
||||||
}
|
|
||||||
public static List<String> getList(ServerPlayerEntity player, HandType type, String setting) {
|
|
||||||
Type listType = new TypeToken<ArrayList<String>>() {}.getType();
|
|
||||||
return new Gson().fromJson(Sit.playerSettings.get(player).get("hand."+type+"."+setting),listType);
|
|
||||||
}
|
|
||||||
public static boolean getBool(ServerPlayerEntity player, HandType type, String setting) {
|
|
||||||
return Boolean.parseBoolean(Sit.playerSettings.get(player).get("hand."+type+"."+setting));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue