use of wraith.waystones.block.WaystoneBlock 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 wraith.waystones.block.WaystoneBlock in project FabricWaystones by LordDeatHunter.
the class WaystoneScrollItem method useOnBlock.
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
if (context.getWorld().getBlockState(context.getBlockPos()).getBlock() instanceof WaystoneBlock && context.getPlayer() != null) {
HashSet<String> discovered = ((PlayerEntityMixinAccess) context.getPlayer()).getDiscoveredWaystones();
ItemStack stack = context.getStack();
if (discovered.isEmpty()) {
return ActionResult.FAIL;
}
NbtCompound tag = new NbtCompound();
NbtList list = new NbtList();
for (String hash : discovered) {
list.add(NbtString.of(hash));
}
tag.put("waystones", list);
stack.setNbt(tag);
}
return super.useOnBlock(context);
}
Aggregations