Jump to content

[1.12.2] (Server-Client Messaging) Need a second pair of eyes.


KittenKoder

Recommended Posts

So I wrote the class, everything seems to work, but the data saved to the NBT is incorrect, nor does it load the same data from the NBT. Specifically on the sides and facing data. It continually reads and writes the default data set during construction. On the client is seems to save all 0s for the index values. So I know I missed setting the values somewhere, but I can't see it. I know the server is getting the correct updates from the client's GUI, and it sending the updates to the client (to update the GUI correctly). Everything is registered correctly, so please check to see if you can find where I messed up the setting of these values.

 

package com.kittenkoder.magitech.tile;

import java.util.LinkedList;

import javax.annotation.Nullable;

import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

import com.kittenkoder.magitech.Magitech;
import com.kittenkoder.magitech.gui.MagitechGUISorter;
import com.kittenkoder.magitech.network.MagitechRequestSorterUpdate;
import com.kittenkoder.magitech.network.MagitechUpdateIntegerPacket;
import com.kittenkoder.magitech.network.MagitechUpdateSorterPacket;
import com.kittenkoder.magitech.network.containers.UpdatePostData;

public class MagitechTileEntitySorter extends MagitechTileEntityBox implements IMagitechTileEntityIntegerUpdate {
	public static enum Job {
		OUTPUT(0),
		SORT_1(1),
		SORT_2(2),
		SORT_3(3),
		SORT_4(4),
		INPUT(5);
		
		public static final Job[] LOOKUP = new Job[MagitechTileEntitySorter.Job.values().length];
		public final int _index;
		
		Job(int i) {
			this._index = i;
		}
		
		static {
			for(Job job : Job.values())
				Job.LOOKUP[job._index] = job;
		}
	}
	
	public static enum Facing {
		NORTH(0, EnumFacing.NORTH),
		SOUTH(1, EnumFacing.SOUTH),
		EAST(2, EnumFacing.EAST),
		WEST(3, EnumFacing.WEST),
		UP(4, EnumFacing.UP),
		DOWN(5, EnumFacing.DOWN);
		
		private static final Facing[] LOOKUP = new Facing[Facing.values().length];
		public final EnumFacing _face;
		public final int _index;
		private Facing _next;
		
		Facing(int i, EnumFacing f) {
			this._face = f;
			this._index = i;
		}
		
		public Facing getNext() { return this._next; } 
		
		static {
			for(Facing face : Facing.values())
				Facing.LOOKUP[face._index] = face;
			for(int i = 0; i < Facing.LOOKUP.length; i++)
				Facing.LOOKUP[i]._next = i == 0 ? Facing.LOOKUP[Facing.LOOKUP.length - 1] : Facing.LOOKUP[i - 1];
		}
		
		public static int indexForFacing(EnumFacing facing) {
			for(Facing face : LOOKUP) if(face._face == facing) return face._index;
			return 0;
		}
		
		public static Facing facingForFacing(EnumFacing facing) {
			for(Facing face : LOOKUP) if(face._face == facing) return face;
			return LOOKUP[0];
		}
		
		public static Facing lookUp(int i) {
			if(i > 5 || 1 < 0) return LOOKUP[0];
			return LOOKUP[i];
		}
	}
	
	protected static final LinkedList<UpdatePostData> UPDATE_LIST = new LinkedList<UpdatePostData>();
	protected final ItemStackHandler[] _filters;
	protected final Facing[] _sides;
	protected MagitechGUISorter _gui = null;
	
	public MagitechTileEntitySorter() {
		super(4);
		this._filters = new ItemStackHandler[4];
		for(int i = 0; i < 4; i++) this._filters[i] = new ItemStackHandler(3);
		this._sides = new Facing[6];
		for(int i = 0; i < 6; i++) this._sides[i] = Facing.LOOKUP[i];
	}

	@Override
	public void onLoad() {
		if(!world.isRemote) world.scheduleBlockUpdate(pos, this.getBlockType(), 5, 0);
		else Magitech.net.sendToServer(new MagitechRequestSorterUpdate(this));
	}

	public void setGUI(MagitechGUISorter gui) { this._gui = gui; }
	
	public void setJob(Job j, Facing i) {
		this.setJob(j._index, i);
	}

	public void setJob(int j, Facing i) {
		this._sides[j] = i;
		System.out.println("Set to " + Integer.toString(j) + " = " + Integer.toString(this._sides[j]._index));
		if(!world.isRemote) Magitech.net.sendToAllAround(new MagitechUpdateSorterPacket(this), new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 64));
		else if(this._gui != null) this._gui.updateButton(Job.LOOKUP[j], i);
	}

	public void setJob(int j, int i) {
		this.setJob(j, MagitechTileEntitySorter.Facing.lookUp(i));
	}

	public Facing getFacing(Job i) {
		return this._sides[i._index];
	}

	public Facing getFacing(int i) {
		return this._sides[i];
	}

	public void updateTick(IBlockState state) {
		
//		if(this.getBlockType() == null) return;
//		MagitechBlockPipe tblock = (MagitechBlockPipe)this.getBlockType();
//		EnumFacing face = MagitechBlockPipe.getFacingFromMeta(this.getBlockMetadata());
//		BlockPos npos = pos.offset(face);
//		TileEntity entity = world.getTileEntity(npos);
//		if(entity != null && entity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite())) {
//			IItemHandler target = entity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
//			boolean done = false;
//			for(int i = 0; i < inventory.getSlots() && !done; ++i) {
//				ItemStack test = inventory.extractItem(i, tblock.getSpeed(), true);
//				if(test != null && !test.isEmpty()) {
//					int start = test.getCount();
//					test = MagitechUtils.addToInventory(test, target);
//					int end = test.getCount();
//					if(end != start) {
//						inventory.extractItem(i, start - end, false);
//						this.markDirty();
//						entity.markDirty();
//						done = true;
//					}
//				}
//			}
//		}
	}
	
	@Override
	public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
		if(this.inventory == null) return super.hasCapability(capability, facing);
		if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
			return (this._sides[Job.INPUT._index] == Facing.facingForFacing(facing));
		return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
	}
	
	@SuppressWarnings("unchecked")
	@Nullable
	@Override
	public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
		if(this.inventory == null) return super.getCapability(capability, facing);
		if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
			Facing index = Facing.facingForFacing(facing);
			if (this._sides[Job.INPUT._index] == index) return (T)this.inventory;
		}
		return super.getCapability(capability, facing);
	}

	@Override
	public ItemStack getStackInSlot(int index) {
		if(index > this._size) return ItemStack.EMPTY;
		return this.inventory.getStackInSlot(index);
	}

	@Override
	public ItemStack removeStackFromSlot(int index) {
		if(index > this._size) return ItemStack.EMPTY;
		ItemStack stack = this.inventory.getStackInSlot(index);
		this.inventory.setStackInSlot(index, ItemStack.EMPTY);
		return stack;
	}

	@Override
	public void setInventorySlotContents(int index, ItemStack stack) {
		if(index > this._size) return;
		this.inventory.setStackInSlot(index, stack);
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		compound = super.writeToNBT(compound);
		this.writeInventoryToNBT(compound);
		return compound;
	}
	
	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.readInventoryFromNBT(compound);
	}

	@Override
	public void writeInventoryToNBT(NBTTagCompound tag) {
		tag.setTag("MagitechInventory", this.inventory.serializeNBT());
		NBTTagCompound filters = new NBTTagCompound();
		for(int i = 0; i < 4; i++) 
			filters.setTag("Filter_" + Integer.toString(i), this._filters[i].serializeNBT());
		tag.setTag("MagitechFilters", filters);
		filters = new NBTTagCompound();
		for(int i = 0; i < 6; i++) {
			filters.setInteger("Job_" + Integer.toString(i), this._sides[i]._index);
			System.out.println(Integer.toString(i) + " = " + Integer.toString(this._sides[i]._index));
		}
		tag.setTag("MagitechJobs", filters);
	}

	@Override
	public void readInventoryFromNBT(NBTTagCompound tag) {
		this.inventory.deserializeNBT(tag.getCompoundTag("MagitechInventory"));
		NBTTagCompound filters = tag.getCompoundTag("MagitechFilters");
		for(int i = 0; i < 4; i++)
			this._filters[i].deserializeNBT(filters.getCompoundTag("Filter_" + Integer.toString(i)));
		filters = tag.getCompoundTag("MagitechJobs");
		for(int i = 0; i < 6; i++) {
			this._sides[i] = Facing.LOOKUP[filters.getInteger("Job_" + Integer.toString(i))];
			System.out.println(Integer.toString(i) + " = " + Integer.toString(this._sides[i]._index));
		}
	}

	@Override
	public ItemStackHandler getInventory() {
		return this.inventory;
	}
	
	public ItemStackHandler getFilter(int i) {
		return this._filters[i];
	}

	@Override
	public void setIntegerToUpdate(int i, int flag, boolean s) {
		if(this.world.isRemote)
			if(!s) {
				this.setJob(i, this._sides[i]._next);
			} else {
				MagitechTileEntitySorter.UPDATE_LIST.add(new UpdatePostData(this, i, flag));
				Magitech.net.sendToServer(new MagitechUpdateIntegerPacket(this));
			}
		else {
			this.setJob(i, this._sides[i]._next);
		}
	}

	@Override
	public int getNextUpdateInteger() {
		UpdatePostData n = MagitechTileEntitySorter.UPDATE_LIST.getFirst();
		return n.value;
	}

	@Override
	public int getNextUpdateFlag() {
		UpdatePostData n = MagitechTileEntitySorter.UPDATE_LIST.getFirst();
		return n.flag;
	}
	
	@Override
	public void popNext() {
		MagitechTileEntitySorter.UPDATE_LIST.remove();
	}
}

 

I hope this isn't a case where I find it right after I post this. lol Once I get this working I have to generalize much of the code for it as it will be needed for other machines.

Edited by KittenKoder
Link to comment
Share on other sites

So here is how I register the message:

Magitech.net.registerMessage(new MagitechUpdateIntegerPacket.Handler(), MagitechUpdateIntegerPacket.class, id++, Side.SERVER);

Which seems fine, but for some reason the client is the one processing the message. Based on what I read, the server should be processing this not the client. I don't get any errors in the code when it's run, so I can't figure out why this part isn't working as expected.

Link to comment
Share on other sites

... and yet again I feel like an ideeot! It had nothing to do with that class, it was the message processing class that was in error. It was looking at the wrong world instance, serves me right for using copy-pasta even when it is my own code. So for those running into similar issues, remember to get the correct world instances when collecting the TileEntity to modify in a message.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.