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

94 lines
4.1 KiB
Java
Raw Normal View History

2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.network;
2016-08-09 20:56:09 +02:00
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.booklet.chapter.BookletChapterTrials;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.data.PlayerData;
import de.ellpeck.actuallyadditions.common.data.PlayerData.PlayerSave;
2016-08-09 20:56:09 +02:00
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
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
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public static void sendButtonPacket(TileEntity tile, int buttonId) {
2016-08-09 20:56:09 +02:00
NBTTagCompound compound = new NBTTagCompound();
BlockPos pos = tile.getPos();
compound.setInteger("X", pos.getX());
compound.setInteger("Y", pos.getY());
compound.setInteger("Z", pos.getZ());
compound.setInteger("WorldID", tile.getWorld().provider.getDimension());
2016-11-26 21:32:27 +01:00
compound.setInteger("PlayerID", Minecraft.getMinecraft().player.getEntityId());
2016-08-09 20:56:09 +02:00
compound.setInteger("ButtonID", buttonId);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
}
2019-05-02 09:10:29 +02:00
public static void syncPlayerData(EntityPlayer player, boolean log) {
2016-08-09 20:56:09 +02:00
NBTTagCompound compound = new NBTTagCompound();
compound.setBoolean("Log", log);
NBTTagCompound data = new NBTTagCompound();
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);
2019-05-02 09:10:29 +02:00
if (player instanceof EntityPlayerMP) {
PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.SYNC_PLAYER_DATA), (EntityPlayerMP) player);
}
}
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public static void sendPlayerDataToServer(boolean log, int type) {
NBTTagCompound compound = new NBTTagCompound();
compound.setBoolean("Log", log);
compound.setInteger("Type", type);
EntityPlayer player = Minecraft.getMinecraft().player;
2019-05-02 09:10:29 +02:00
if (player != null) {
compound.setInteger("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) {
compound.setBoolean("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) {
2017-02-18 00:54:58 +01:00
compound.setBoolean("Achievement", true);
}
}
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.PLAYER_DATA_TO_SERVER));
2016-08-09 20:56:09 +02:00
}
}
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public static void sendNumberPacket(TileEntity tile, double number, int id) {
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("X", tile.getPos().getX());
compound.setInteger("Y", tile.getPos().getY());
compound.setInteger("Z", tile.getPos().getZ());
compound.setInteger("WorldID", tile.getWorld().provider.getDimension());
compound.setInteger("PlayerID", Minecraft.getMinecraft().player.getEntityId());
compound.setInteger("NumberID", id);
compound.setDouble("Number", number);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_NUMBER_TO_TILE_HANDLER));
}
2016-08-09 20:56:09 +02:00
}