use of wraith.waystones.access.PlayerEntityMixinAccess 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.access.PlayerEntityMixinAccess in project FabricWaystones by LordDeatHunter.
the class WaystonesEventManager method registerEvents.
public static void registerEvents() {
ServerLifecycleEvents.SERVER_STARTED.register((server) -> {
if (Waystones.WAYSTONE_STORAGE == null) {
Waystones.WAYSTONE_STORAGE = new WaystoneStorage(server);
}
});
ServerLifecycleEvents.SERVER_STOPPED.register((server) -> {
Waystones.WAYSTONE_STORAGE.loadOrSaveWaystones(true);
Waystones.WAYSTONE_STORAGE = null;
});
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
PacketByteBuf data = PacketByteBufs.create();
data.writeNbt(Config.getInstance().toNbtCompound());
ServerPlayNetworking.send(handler.player, Utils.ID("waystone_config_update"), data);
Waystones.WAYSTONE_STORAGE.sendToPlayer(handler.player);
Waystones.WAYSTONE_STORAGE.sendCompatData(handler.player);
});
ServerLifecycleEvents.SERVER_STARTING.register(WaystonesWorldgen::registerVanillaVillageWorldgen);
ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> ((PlayerEntityMixinAccess) newPlayer).syncData());
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(CommandManager.literal("waystones").then(CommandManager.literal("reload").requires(source -> source.hasPermissionLevel(1)).executes(context -> {
Config.getInstance().loadConfig();
PacketByteBuf data = PacketByteBufs.create();
data.writeNbt(Config.getInstance().toNbtCompound());
for (ServerPlayerEntity player : context.getSource().getServer().getPlayerManager().getPlayerList()) {
ServerPlayNetworking.send(player, Utils.ID("waystone_config_update"), data);
}
ServerPlayerEntity player = context.getSource().getPlayer();
if (player != null) {
player.sendMessage(new LiteralText("§6[§eWaystones§6] §3has successfully reloaded!"), false);
}
return 1;
})).then(CommandManager.literal("display").executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayer();
if (player == null) {
return 1;
}
Config.getInstance().print(player);
return 1;
}))));
}
use of wraith.waystones.access.PlayerEntityMixinAccess in project FabricWaystones by LordDeatHunter.
the class WaystonesClient method registerPacketHandlers.
private void registerPacketHandlers() {
ClientPlayNetworking.registerGlobalReceiver(Utils.ID("waystone_packet"), (client, networkHandler, data, sender) -> {
var nbt = data.readNbt();
client.execute(() -> {
if (Waystones.WAYSTONE_STORAGE == null) {
Waystones.WAYSTONE_STORAGE = new WaystoneStorage(null);
}
Waystones.WAYSTONE_STORAGE.fromTag(nbt);
if (client.player == null) {
return;
}
HashSet<String> toForget = new HashSet<>();
for (String hash : ((PlayerEntityMixinAccess) client.player).getDiscoveredWaystones()) {
if (!Waystones.WAYSTONE_STORAGE.containsHash(hash)) {
toForget.add(hash);
}
}
((PlayerEntityMixinAccess) client.player).forgetWaystones(toForget);
if (client.player.currentScreenHandler instanceof UniversalWaystoneScreenHandler) {
((UniversalWaystoneScreenHandler) client.player.currentScreenHandler).updateWaystones(client.player);
}
});
});
ClientPlayNetworking.registerGlobalReceiver(Utils.ID("waystone_config_update"), (client, networkHandler, data, sender) -> {
NbtCompound tag = data.readNbt();
client.execute(() -> Config.getInstance().loadConfig(tag));
});
ClientPlayNetworking.registerGlobalReceiver(Utils.ID("sync_player"), (client, networkHandler, data, sender) -> {
NbtCompound tag = data.readNbt();
client.execute(() -> {
if (client.player != null) {
((PlayerEntityMixinAccess) client.player).fromTagW(tag);
}
});
});
}
use of wraith.waystones.access.PlayerEntityMixinAccess 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);
}
use of wraith.waystones.access.PlayerEntityMixinAccess in project FabricWaystones by LordDeatHunter.
the class ScrollOfInfiniteKnowledgeItem method use.
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack stack = user.getStackInHand(hand);
if (Waystones.WAYSTONE_STORAGE == null) {
return TypedActionResult.fail(stack);
}
int learned = 0;
HashSet<String> toLearn = new HashSet<>();
for (String hash : Waystones.WAYSTONE_STORAGE.getAllHashes()) {
if (!((PlayerEntityMixinAccess) user).hasDiscoveredWaystone(hash)) {
var waystone = Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash);
if (waystone != null && waystone.getOwner() == null) {
waystone.setOwner(user);
}
toLearn.add(hash);
++learned;
}
}
Text text;
if (learned > 0) {
if (learned > 1) {
text = new TranslatableText("waystones.learned.infinite.multiple", new LiteralText(String.valueOf(learned)).styled(style -> style.withColor(TextColor.parse(new TranslatableText("waystones.learned.infinite.multiple.arg_color").getString()))));
} else {
text = new TranslatableText("waystones.learned.infinite.single");
}
((PlayerEntityMixinAccess) user).discoverWaystones(toLearn);
if (!user.isCreative() && Config.getInstance().consumeInfiniteScroll()) {
stack.decrement(1);
}
} else {
text = new TranslatableText("waystones.learned.infinite.none");
}
if (!world.isClient) {
user.sendMessage(text, false);
}
if (stack.isEmpty()) {
user.setStackInHand(hand, ItemStack.EMPTY);
}
stack = user.getStackInHand(hand);
return TypedActionResult.success(stack, world.isClient());
}
Aggregations