use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.
the class WaystonePacketHandler method registerPacketHandlers.
public static void registerPacketHandlers() {
ServerPlayNetworking.registerGlobalReceiver(REMOVE_WAYSTONE_OWNER, (server, player, networkHandler, data, sender) -> {
NbtCompound tag = data.readNbt();
server.execute(() -> {
if (tag == null || !tag.contains("waystone_hash") || !tag.contains("waystone_owner")) {
return;
}
String hash = tag.getString("waystone_hash");
UUID owner = tag.getUuid("waystone_owner");
if ((player.getUuid().equals(owner) || player.hasPermissionLevel(2))) {
Waystones.WAYSTONE_STORAGE.setOwner(hash, null);
}
});
});
ServerPlayNetworking.registerGlobalReceiver(WAYSTONE_GUI_SLOT_CLICK, (server, player, networkHandler, data, sender) -> {
NbtCompound tag = data.readNbt();
server.execute(() -> {
if (tag == null || !tag.contains("sync_id") || !tag.contains("clicked_slot")) {
return;
}
int syncId = tag.getInt("sync_id");
int button = tag.getInt("clicked_slot");
if (player.currentScreenHandler.syncId == syncId) {
player.currentScreenHandler.onButtonClick(player, button);
}
});
});
ServerPlayNetworking.registerGlobalReceiver(RENAME_WAYSTONE, (server, player, networkHandler, data, sender) -> {
NbtCompound tag = data.readNbt();
if (tag == null || !tag.contains("waystone_name") || !tag.contains("waystone_hash") || !tag.contains("waystone_owner")) {
return;
}
String name = tag.getString("waystone_name");
String hash = tag.getString("waystone_hash");
UUID owner = tag.getUuid("waystone_owner");
server.execute(() -> {
if (Waystones.WAYSTONE_STORAGE.containsHash(hash) && ((player.getUuid().equals(owner) && owner.equals(Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash).getOwner())) || player.hasPermissionLevel(2))) {
Waystones.WAYSTONE_STORAGE.renameWaystone(hash, name);
}
});
});
ServerPlayNetworking.registerGlobalReceiver(FORGET_WAYSTONE, (server, player, networkHandler, data, sender) -> {
NbtCompound tag = data.readNbt();
if (tag == null || !tag.contains("waystone_hash")) {
return;
}
String hash = tag.getString("waystone_hash");
server.execute(() -> {
var waystone = Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash);
if (waystone == null || waystone.isGlobal()) {
return;
}
if (player.getUuid().equals(waystone.getOwner())) {
waystone.setOwner(null);
}
((PlayerEntityMixinAccess) player).forgetWaystone(hash);
});
});
ServerPlayNetworking.registerGlobalReceiver(REQUEST_PLAYER_SYNC, (server, player, networkHandler, data, sender) -> server.execute(((PlayerEntityMixinAccess) player)::syncData));
ServerPlayNetworking.registerGlobalReceiver(TOGGLE_GLOBAL_WAYSTONE, (server, player, networkHandler, data, sender) -> {
NbtCompound tag = data.readNbt();
if (tag == null || !tag.contains("waystone_hash") || !tag.contains("waystone_owner")) {
return;
}
if (!Config.getInstance().canPlayersToggleGlobal() && !player.hasPermissionLevel(2)) {
return;
}
server.execute(() -> {
String hash = tag.getString("waystone_hash");
UUID owner = tag.getUuid("waystone_owner");
if (Waystones.WAYSTONE_STORAGE.containsHash(hash) && ((player.getUuid().equals(owner) && owner.equals(Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash).getOwner())) || player.hasPermissionLevel(2))) {
Waystones.WAYSTONE_STORAGE.toggleGlobal(hash);
}
});
});
ServerPlayNetworking.registerGlobalReceiver(SYNC_PLAYER_FROM_CLIENT, (server, player, networkHandler, data, sender) -> {
NbtCompound tag = data.readNbt();
server.execute(() -> ((PlayerEntityMixinAccess) player).fromTagW(tag));
});
ServerPlayNetworking.registerGlobalReceiver(TELEPORT_TO_WAYSTONE, (server, player, networkHandler, data, sender) -> {
NbtCompound tag = data.readNbt();
if (tag == null || !tag.contains("waystone_hash")) {
return;
}
server.execute(() -> {
if (!tag.contains("waystone_hash")) {
return;
}
String hash = tag.getString("waystone_hash");
var waystone = Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash);
if (waystone == null) {
return;
}
if (waystone.getWorld() != null && !(waystone.getWorld().getBlockState(waystone.getPos()).getBlock() instanceof WaystoneBlock)) {
Waystones.WAYSTONE_STORAGE.removeWaystone(hash);
waystone.getWorld().removeBlockEntity(waystone.getPos());
} else {
waystone.teleportPlayer(player, true);
}
});
});
}
use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.
the class WaystoneStorage method fromTag.
public void fromTag(NbtCompound tag) {
if (tag == null || !tag.contains("waystones")) {
return;
}
WAYSTONES.clear();
var globals = new HashSet<String>();
for (var element : tag.getList("global_waystones", NbtElement.STRING_TYPE)) {
globals.add(element.asString());
}
var waystones = tag.getList("waystones", NbtElement.COMPOUND_TYPE);
for (int i = 0; i < waystones.size(); ++i) {
NbtCompound waystoneTag = waystones.getCompound(i);
if (!waystoneTag.contains("hash") || !waystoneTag.contains("name") || !waystoneTag.contains("dimension") || !waystoneTag.contains("position")) {
continue;
}
String name = waystoneTag.getString("name");
String hash = waystoneTag.getString("hash");
String dimension = waystoneTag.getString("dimension");
int[] coordinates = waystoneTag.getIntArray("position");
BlockPos pos = new BlockPos(coordinates[0], coordinates[1], coordinates[2]);
WAYSTONES.put(hash, new Lazy(name, pos, hash, dimension, globals.contains(hash)));
}
}
use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.
the class WaystoneStorage method loadOrSaveWaystones.
public void loadOrSaveWaystones(boolean save) {
if (server == null) {
return;
}
ServerWorld world = server.getWorld(ServerWorld.OVERWORLD);
if (save) {
state.markDirty();
sendToAllPlayers();
} else {
try {
NbtCompound compoundTag = world.getPersistentStateManager().readNbt(ID, SharedConstants.getGameVersion().getWorldVersion());
state.writeNbt(compoundTag.getCompound("data"));
} catch (IOException ignored) {
}
}
world.getPersistentStateManager().save();
}
use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.
the class WaystoneStorage method toTag.
public NbtCompound toTag(NbtCompound tag) {
if (tag == null) {
tag = new NbtCompound();
}
NbtList waystones = new NbtList();
for (Map.Entry<String, WaystoneValue> waystone : WAYSTONES.entrySet()) {
String hash = waystone.getKey();
WaystoneValue entity = waystone.getValue();
NbtCompound waystoneTag = new NbtCompound();
waystoneTag.putString("hash", hash);
waystoneTag.putString("name", entity.getWaystoneName());
BlockPos pos = entity.way_getPos();
waystoneTag.putIntArray("position", Arrays.asList(pos.getX(), pos.getY(), pos.getZ()));
waystoneTag.putString("dimension", entity.getWorldName());
waystones.add(waystoneTag);
}
tag.put("waystones", waystones);
NbtList globals = new NbtList();
var globalWaystones = getGlobals();
for (String globalWaystone : globalWaystones) {
globals.add(NbtString.of(globalWaystone));
}
tag.put("global_waystones", globals);
return tag;
}
use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.
the class CompatibilityLayer method writeNbt.
public NbtCompound writeNbt(NbtCompound tag) {
if (COMPATABILITY_ENABLED)
return tag;
if (tag == null)
tag = new NbtCompound();
NbtList compatWaystones = new NbtList();
for (Map.Entry<String, HashSet<String>> player : COMPATABILITY_MAP.entrySet()) {
NbtCompound playerTag = new NbtCompound();
playerTag.putString("username", player.getKey());
NbtList knownWaystones = new NbtList();
for (String hash : player.getValue()) {
NbtCompound waystone = new NbtCompound();
waystone.putString("hash", hash);
knownWaystones.add(waystone);
}
playerTag.put("waystones", knownWaystones);
compatWaystones.add(playerTag);
}
tag.put("waystones_compat", compatWaystones);
return tag;
}
Aggregations