mirror of
https://github.com/Oth3r/Sit.git
synced 2025-09-19 16:03:22 +02:00
per player hand restrictions
* optimized code
This commit is contained in:
parent
614fddb6bd
commit
43c72ce1ba
7 changed files with 159 additions and 26 deletions
|
@ -12,7 +12,7 @@ import net.minecraft.entity.Entity;
|
|||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.entity.decoration.DisplayEntity;
|
||||
import net.minecraft.item.BlockItem;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.Text;
|
||||
|
@ -23,6 +23,7 @@ import net.minecraft.util.math.BlockPos;
|
|||
import net.minecraft.util.math.Box;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.World;
|
||||
import one.oth3r.sit.Utl.HandType;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
@ -36,31 +37,34 @@ public class Events {
|
|||
food.add(UseAction.DRINK);
|
||||
ArrayList<UseAction> notUsable = new ArrayList<>(food);
|
||||
notUsable.add(UseAction.NONE);
|
||||
Item mainItem = player.getMainHandStack().getItem();
|
||||
Item offItem = player.getOffHandStack().getItem();
|
||||
HashMap<HandType, ItemStack> itemMap = new HashMap<>();
|
||||
itemMap.put(HandType.main,player.getMainHandStack());
|
||||
itemMap.put(HandType.off,player.getOffHandStack());
|
||||
// if sneaking cant sit
|
||||
if (player.isSneaking()) return false;
|
||||
if (config.mainReq.equals(config.HandRequirements.empty) && !player.getMainHandStack().isEmpty()) return false;
|
||||
if (config.mainReq.equals(config.HandRequirements.restrictive)) {
|
||||
if (checkList(config.mainBlacklist,mainItem)) return false;
|
||||
if (!checkList(config.mainWhitelist,mainItem)) {
|
||||
if (config.mainBlock && (mainItem instanceof BlockItem)) return false;
|
||||
if (config.mainFood && food.contains(player.getMainHandStack().getUseAction())) return false;
|
||||
if (config.mainUsable && !notUsable.contains(player.getMainHandStack().getUseAction())) return false;
|
||||
}
|
||||
}
|
||||
if (config.offReq.equals(config.HandRequirements.empty) && !player.getOffHandStack().isEmpty()) return false;
|
||||
if (config.offReq.equals(config.HandRequirements.restrictive)) {
|
||||
if (checkList(config.offBlacklist,offItem)) return false;
|
||||
if (!checkList(config.offWhitelist,offItem)) {
|
||||
if (config.offBlock && (offItem instanceof BlockItem)) return false;
|
||||
if (config.offFood && food.contains(player.getOffHandStack().getUseAction())) return false;
|
||||
if (config.offUsable && !notUsable.contains(player.getOffHandStack().getUseAction())) return false;
|
||||
// for both hands
|
||||
for (HandType type:HandType.values()) {
|
||||
ItemStack targetStack = itemMap.get(type);
|
||||
// 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 req is restrictive
|
||||
if (Utl.getReq(player,type).equals(config.HandRequirement.restrictive)) {
|
||||
// if item is in blacklist, false
|
||||
if (checkList(Utl.getList(player,type,"blacklist"),targetStack)) return false;
|
||||
// if item is NOT in whitelist
|
||||
if (!checkList(Utl.getList(player,type,"whitelist"),targetStack)) {
|
||||
// if block is restricted and items is block, false, ect
|
||||
if (Utl.getBool(player,type,"blocks") && (targetStack.getItem() instanceof BlockItem)) return false;
|
||||
if (Utl.getBool(player,type,"food") && food.contains(targetStack.getUseAction())) return false;
|
||||
if (Utl.getBool(player,type,"usable") && !notUsable.contains(targetStack.getUseAction())) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
// else true
|
||||
return true;
|
||||
}
|
||||
public static boolean checkList(List<String> list, Item item) {
|
||||
String itemID = Registries.ITEM.getId(item).toString();
|
||||
public static boolean checkList(List<String> list, ItemStack itemStack) {
|
||||
String itemID = Registries.ITEM.getId(itemStack.getItem()).toString();
|
||||
return list.contains(itemID);
|
||||
}
|
||||
public static HashMap<String,HashMap<String,Object>> getCustomBlocks() {
|
||||
|
@ -154,9 +158,12 @@ public class Events {
|
|||
}
|
||||
public static void register() {
|
||||
ServerTickEvents.END_SERVER_TICK.register(minecraftServer -> minecraftServer.execute(Events::cleanUp));
|
||||
// PLAYER JOIN
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
||||
ServerPlayerEntity player = handler.player;
|
||||
checkPlayers.put(player,2);
|
||||
// put server settings in the player settings
|
||||
Sit.playerSettings.put(player,Utl.getHandSettings());
|
||||
});
|
||||
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
|
||||
ServerPlayerEntity player = handler.player;
|
||||
|
@ -168,6 +175,7 @@ public class Events {
|
|||
entities.remove(player);
|
||||
}
|
||||
checkPlayers.remove(player);
|
||||
Sit.playerSettings.remove(player);
|
||||
});
|
||||
ServerLifecycleEvents.SERVER_STARTED.register(s -> {
|
||||
Sit.server = s;
|
||||
|
|
|
@ -90,14 +90,14 @@ public class ModMenu implements ModMenuApi {
|
|||
.category(ConfigCategory.createBuilder()
|
||||
.name(lang("category.main_hand"))
|
||||
.tooltip(lang("category.main_hand.tooltip"))
|
||||
.option(Option.<config.HandRequirements>createBuilder()
|
||||
.option(Option.<config.HandRequirement>createBuilder()
|
||||
.name(lang("hand.requirements"))
|
||||
.description(OptionDescription.of(lang("hand.requirements.description")
|
||||
.append("\n\n").append(lang("hand.requirements.description_2").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.AQUA))))
|
||||
.append("\n").append(lang("hand.requirements.description_3").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN))))
|
||||
.append("\n").append(lang("hand.requirements.description_4").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.RED))))))
|
||||
.binding(config.defaults.mainReq, () -> config.mainReq, n -> config.mainReq = n)
|
||||
.controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirements.class)
|
||||
.controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirement.class)
|
||||
.formatValue(v -> Text.translatable("config.sit."+v.name().toLowerCase())))
|
||||
.build())
|
||||
.group(OptionGroup.createBuilder()
|
||||
|
@ -142,14 +142,14 @@ public class ModMenu implements ModMenuApi {
|
|||
.category(ConfigCategory.createBuilder()
|
||||
.name(lang("category.off_hand"))
|
||||
.tooltip(lang("category.off_hand.tooltip"))
|
||||
.option(Option.<config.HandRequirements>createBuilder()
|
||||
.option(Option.<config.HandRequirement>createBuilder()
|
||||
.name(lang("hand.requirements"))
|
||||
.description(OptionDescription.of(lang("hand.requirements.description")
|
||||
.append("\n\n").append(lang("hand.requirements.description_2").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.AQUA))))
|
||||
.append("\n").append(lang("hand.requirements.description_3").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN))))
|
||||
.append("\n").append(lang("hand.requirements.description_4").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.RED))))))
|
||||
.binding(config.defaults.offReq, () -> config.offReq, n -> config.offReq = n)
|
||||
.controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirements.class)
|
||||
.controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirement.class)
|
||||
.formatValue(v -> Text.translatable("config.sit."+v.name().toLowerCase())))
|
||||
.build())
|
||||
.group(OptionGroup.createBuilder()
|
||||
|
|
34
src/main/java/one/oth3r/sit/PacketBuilder.java
Normal file
34
src/main/java/one/oth3r/sit/PacketBuilder.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package one.oth3r.sit;
|
||||
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class PacketBuilder {
|
||||
public static final String SETTINGS = "settings_v1.0";
|
||||
private final String message;
|
||||
private PacketByteBuf packetByteBuf = PacketByteBufs.create();
|
||||
public PacketBuilder(PacketByteBuf buf) {
|
||||
// Read any data sent in the packet
|
||||
message = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(buf.array())).toString().trim();
|
||||
packetByteBuf = buf;
|
||||
}
|
||||
public PacketBuilder(String message) {
|
||||
this.message = message;
|
||||
packetByteBuf.writeBytes(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)).array());
|
||||
}
|
||||
public static Identifier getIdentifier() {
|
||||
// only 1 packet rn
|
||||
return new Identifier(Sit.MOD_ID, SETTINGS);
|
||||
}
|
||||
public void send() {
|
||||
ClientPlayNetworking.send(getIdentifier(), packetByteBuf);
|
||||
}
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
}
|
|
@ -1,16 +1,26 @@
|
|||
package one.oth3r.sit;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.command.CommandManager;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.text.Text;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Sit implements ModInitializer {
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger("sit");
|
||||
public static final String MOD_ID = "oth3r-sit";
|
||||
public static HashMap<ServerPlayerEntity, HashMap<String,String>> playerSettings = new HashMap<>();
|
||||
public static final String ENTITY_NAME = "-sit!-entity-";
|
||||
public static MinecraftServer server;
|
||||
public static CommandManager commandManager;
|
||||
|
@ -23,6 +33,15 @@ public class Sit implements ModInitializer {
|
|||
// inner stair offset & custom support for that ig
|
||||
config.load();
|
||||
Events.register();
|
||||
//PACKETS
|
||||
ServerPlayNetworking.registerGlobalReceiver(PacketBuilder.getIdentifier(),
|
||||
(server, player, handler, buf, responseSender) -> server.execute(() -> {
|
||||
PacketBuilder packet = new PacketBuilder(buf);
|
||||
Type hashMapToken = new TypeToken<HashMap<String, Object>>() {}.getType();
|
||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||
playerSettings.put(player,gson.fromJson(packet.getMessage(),hashMapToken));
|
||||
System.out.println(playerSettings);
|
||||
}));
|
||||
}
|
||||
public static MutableText lang(String key, Object... args) {
|
||||
if (isClient) return Text.translatable(key, args);
|
||||
|
|
24
src/main/java/one/oth3r/sit/SitClient.java
Normal file
24
src/main/java/one/oth3r/sit/SitClient.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package one.oth3r.sit;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
||||
|
||||
public class SitClient implements ClientModInitializer {
|
||||
public static boolean inGame = false;
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
|
||||
inGame = true;
|
||||
// send a data packet whenever joining a server
|
||||
client.execute(SitClient::sendPackets);
|
||||
});
|
||||
// reset inGame
|
||||
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> inGame = false);
|
||||
}
|
||||
public static void sendPackets() {
|
||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||
new PacketBuilder(gson.toJson(Utl.getHandSettings())).send();
|
||||
}
|
||||
}
|
45
src/main/java/one/oth3r/sit/Utl.java
Normal file
45
src/main/java/one/oth3r/sit/Utl.java
Normal file
|
@ -0,0 +1,45 @@
|
|||
package one.oth3r.sit;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class Utl {
|
||||
enum HandType {
|
||||
main,
|
||||
off
|
||||
}
|
||||
public static HashMap<String,String> getHandSettings() {
|
||||
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));
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "sit",
|
||||
"id": "oth3r-sit",
|
||||
"version": "${version}",
|
||||
"name": "Sit!",
|
||||
"description": "Adds sitting to minecraft! Endless customizability for hand restrictions and sittable blocks.",
|
||||
|
@ -21,6 +21,9 @@
|
|||
"server": [
|
||||
"one.oth3r.sit.SitServer"
|
||||
],
|
||||
"client": [
|
||||
"one.oth3r.sit.SitClient"
|
||||
],
|
||||
"modmenu": [
|
||||
"one.oth3r.sit.ModMenu"
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue