ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandlerHelper.java

103 lines
4.2 KiB
Java
Raw Normal View History

2016-08-09 20:56:09 +02:00
/*
* This file ("PacketHandlerHelper.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-08-09 20:56:09 +02:00
*/
package de.ellpeck.actuallyadditions.mod.network;
2017-02-18 00:54:58 +01:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.booklet.IBookletChapter;
import de.ellpeck.actuallyadditions.mod.booklet.chapter.BookletChapterTrials;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import de.ellpeck.actuallyadditions.mod.data.PlayerData.PlayerSave;
2016-08-09 20:56:09 +02:00
import net.minecraft.client.Minecraft;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.nbt.CompoundNBT;
2016-08-09 20:56:09 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
2016-08-09 20:56:09 +02:00
2019-05-02 09:10:29 +02:00
public final class PacketHandlerHelper {
2016-08-09 20:56:09 +02:00
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-05-02 09:10:29 +02:00
public static void sendButtonPacket(TileEntity tile, int buttonId) {
2021-02-26 22:15:48 +01:00
CompoundNBT compound = new CompoundNBT();
2016-08-09 20:56:09 +02:00
BlockPos pos = tile.getPos();
2021-02-27 16:33:00 +01:00
compound.putInt("X", pos.getX());
compound.putInt("Y", pos.getY());
compound.putInt("Z", pos.getZ());
compound.putInt("WorldID", tile.getWorld().provider.getDimension());
compound.putInt("PlayerID", Minecraft.getInstance().player.getEntityId());
compound.putInt("ButtonID", buttonId);
PacketHandler.THE_NETWORK.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
2016-08-09 20:56:09 +02:00
}
2021-02-26 22:15:48 +01:00
public static void syncPlayerData(PlayerEntity player, boolean log) {
CompoundNBT compound = new CompoundNBT();
2021-02-27 16:33:00 +01:00
compound.putBoolean("Log", log);
2021-02-26 22:15:48 +01:00
CompoundNBT data = new CompoundNBT();
2016-11-22 19:35:52 +01:00
PlayerData.getDataFromPlayer(player).writeToNBT(data, false);
2016-08-09 20:56:09 +02:00
compound.setTag("Data", data);
2021-02-26 22:15:48 +01:00
if (player instanceof ServerPlayerEntity) {
2021-02-27 16:33:00 +01:00
PacketHandler.THE_NETWORK.sendTo(new PacketServerToClient(compound, PacketHandler.SYNC_PLAYER_DATA), (ServerPlayerEntity) player);
}
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-05-02 09:10:29 +02:00
public static void sendPlayerDataToServer(boolean log, int type) {
2021-02-26 22:15:48 +01:00
CompoundNBT compound = new CompoundNBT();
2021-02-27 16:33:00 +01:00
compound.putBoolean("Log", log);
compound.putInt("Type", type);
2021-02-26 22:15:48 +01:00
PlayerEntity player = Minecraft.getInstance().player;
2019-05-02 09:10:29 +02:00
if (player != null) {
2021-02-27 16:33:00 +01:00
compound.putInt("World", player.world.provider.getDimension());
compound.setUniqueId("UUID", player.getUniqueID());
PlayerSave data = PlayerData.getDataFromPlayer(player);
2019-05-02 09:10:29 +02:00
if (type == 0) {
compound.setTag("Bookmarks", data.saveBookmarks());
2019-05-02 09:10:29 +02:00
} else if (type == 1) {
2021-02-27 16:33:00 +01:00
compound.putBoolean("DidBookTutorial", data.didBookTutorial);
2019-05-02 09:10:29 +02:00
} else if (type == 2) {
2017-02-18 00:54:58 +01:00
compound.setTag("Trials", data.saveTrials());
int total = 0;
2019-05-02 09:10:29 +02:00
for (IBookletChapter chapter : ActuallyAdditionsAPI.entryTrials.getAllChapters()) {
if (chapter instanceof BookletChapterTrials) {
2017-02-18 00:54:58 +01:00
total++;
}
}
2019-05-02 09:10:29 +02:00
if (data.completedTrials.size() >= total) {
2021-02-27 16:33:00 +01:00
compound.putBoolean("Achievement", true);
2017-02-18 00:54:58 +01:00
}
}
2021-02-27 16:33:00 +01:00
PacketHandler.THE_NETWORK.sendToServer(new PacketClientToServer(compound, PacketHandler.PLAYER_DATA_TO_SERVER));
2016-08-09 20:56:09 +02:00
}
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-05-02 09:10:29 +02:00
public static void sendNumberPacket(TileEntity tile, double number, int id) {
2021-02-26 22:15:48 +01:00
CompoundNBT compound = new CompoundNBT();
2021-02-27 16:33:00 +01:00
compound.putInt("X", tile.getPos().getX());
compound.putInt("Y", tile.getPos().getY());
compound.putInt("Z", tile.getPos().getZ());
compound.putInt("WorldID", tile.getWorld().provider.getDimension());
compound.putInt("PlayerID", Minecraft.getInstance().player.getEntityId());
compound.putInt("NumberID", id);
compound.setDouble("Number", number);
2021-02-27 16:33:00 +01:00
PacketHandler.THE_NETWORK.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_NUMBER_TO_TILE_HANDLER));
}
2016-08-09 20:56:09 +02:00
}