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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • OLXTOTO adalah situs bandar togel online resmi terbesar dan terpercaya di Indonesia. Bergabunglah dengan OLXTOTO dan nikmati pengalaman bermain togel yang aman dan terjamin. Koleksi toto 4D dan togel toto terlengkap di OLXTOTO membuat para member memiliki pilihan taruhan yang lebih banyak. Sebagai situs togel terpercaya, OLXTOTO menjaga keamanan dan kenyamanan para membernya dengan sistem keamanan terbaik dan enkripsi data. Transaksi yang cepat, aman, dan terpercaya merupakan jaminan dari OLXTOTO. Nikmati layanan situs toto terbaik dari OLXTOTO dengan tampilan yang user-friendly dan mudah digunakan. Layanan pelanggan tersedia 24/7 untuk membantu para member. Bergabunglah dengan OLXTOTO sekarang untuk merasakan pengalaman bermain togel yang menyenangkan dan menguntungkan.
    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • BD303 merupakan salah satu situs slot mudah scatter paling populer dan digemari oleh kalangan slot online di tahun 2024 mainkan sekarang dengan kesempatan yang mudah menang jackpot jutaan rupiah.
  • Topics

×
×
  • Create New...

Important Information

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