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

110 lines
4.2 KiB
Java
Raw Normal View History

2015-11-24 19:59:33 +01:00
/*
2015-12-30 22:02:15 +01:00
* This file ("PacketBookletStandButton.java") is part of the Actually Additions Mod for Minecraft.
2015-11-24 19:59:33 +01:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-11-24 19:59:33 +01:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
2015-11-24 19:59:33 +01:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.network;
2015-11-24 19:59:33 +01:00
2016-01-05 14:57:50 +01:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.internal.EntrySet;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBookletStand;
2015-11-24 19:59:33 +01:00
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
2016-01-08 13:31:58 +01:00
import net.minecraft.util.BlockPos;
2015-11-24 19:59:33 +01:00
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
2015-11-24 19:59:33 +01:00
import java.util.Objects;
2015-11-24 19:59:33 +01:00
public class PacketBookletStandButton implements IMessage{
private int tileX;
private int tileY;
private int tileZ;
private int worldID;
private int playerID;
private int entryID;
private int chapterID;
private int pageID;
private int pageInIndex;
@SuppressWarnings("unused")
public PacketBookletStandButton(){
}
public PacketBookletStandButton(int x, int y, int z, World world, EntityPlayer player, EntrySet set){
2015-11-24 19:59:33 +01:00
this.tileX = x;
this.tileY = y;
this.tileZ = z;
this.worldID = world.provider.getDimensionId();
2015-11-24 19:59:33 +01:00
this.playerID = player.getEntityId();
2016-01-05 14:57:50 +01:00
this.entryID = set.entry == null ? -1 : ActuallyAdditionsAPI.bookletEntries.indexOf(set.entry);
this.chapterID = set.entry == null || set.chapter == null ? -1 : set.entry.getChapters().indexOf(set.chapter);
this.pageID = set.page == null ? -1 : set.page.getID();
this.pageInIndex = set.pageInIndex;
2015-11-24 19:59:33 +01:00
}
@Override
public void fromBytes(ByteBuf buf){
this.tileX = buf.readInt();
this.tileY = buf.readInt();
this.tileZ = buf.readInt();
this.worldID = buf.readInt();
this.playerID = buf.readInt();
this.chapterID = buf.readInt();
this.pageID = buf.readInt();
this.entryID = buf.readInt();
this.pageInIndex = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf){
buf.writeInt(this.tileX);
buf.writeInt(this.tileY);
buf.writeInt(this.tileZ);
buf.writeInt(this.worldID);
buf.writeInt(this.playerID);
buf.writeInt(this.chapterID);
buf.writeInt(this.pageID);
buf.writeInt(this.entryID);
buf.writeInt(this.pageInIndex);
}
public static class Handler implements IMessageHandler<PacketBookletStandButton, IMessage>{
@Override
public IMessage onMessage(PacketBookletStandButton message, MessageContext ctx){
World world = DimensionManager.getWorld(message.worldID);
2016-01-08 13:31:58 +01:00
TileEntity tile = world.getTileEntity(new BlockPos(message.tileX, message.tileY, message.tileZ));
2015-11-24 19:59:33 +01:00
EntityPlayer player = (EntityPlayer)world.getEntityByID(message.playerID);
if(tile instanceof TileEntityBookletStand){
if(Objects.equals(player.getName(), ((TileEntityBookletStand)tile).assignedPlayer)){
EntrySet theSet = ((TileEntityBookletStand)tile).assignedEntry;
2016-01-05 14:57:50 +01:00
theSet.entry = message.entryID == -1 ? null : ActuallyAdditionsAPI.bookletEntries.get(message.entryID);
theSet.chapter = message.chapterID == -1 || message.entryID == -1 || theSet.entry.getChapters().size() <= message.chapterID ? null : theSet.entry.getChapters().get(message.chapterID);
theSet.page = message.chapterID == -1 || theSet.chapter == null || theSet.chapter.getPages().length <= message.pageID-1 ? null : theSet.chapter.getPages()[message.pageID-1];
theSet.pageInIndex = message.pageInIndex;
((TileEntityBookletStand)tile).sendUpdate();
2015-11-24 19:59:33 +01:00
}
}
return null;
}
}
}