This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Engineer's profile picture

ComputerCraftAPI.registerExternalPeripheral: possible bug

Started by Engineer, 19 January 2014 - 12:18 PM
Engineer #1
Posted 19 January 2014 - 01:18 PM
Hi,

I was coding today for a little tiny mod to experiment for a bit. But there was one thing that I noticed. But first, the code:
CODE
calling the register method

@Mod(modid = ModInfo.ID, name = ModInfo.NAME, version = ModInfo.VERSION, dependencies=ModInfo.DEPENDENCIES)
@NetworkMod(clientSideRequired = ModInfo.clientReq, serverSideRequired = ModInfo.serverReq)
public class test
{
	// Got more code here, but it is a direct call to CC
		@Mod.EventHandler
	public void load(FMLInitializationEvent event)
	{
		ComputerCraftAPI.registerExternalPeripheral(TileEntityDispenser.class, new PeripheralHandler());
	}
}
peripheral handler

public class PeripheralHandler implements IPeripheralHandler
{
	@Override
	public IHostedPeripheral getPeripheral(TileEntity tile)
	{
		return new PeripheralDispenser();
	}
}
PeripheralDispenser

public class PeripheralDispenser implements IHostedPeripheral
{
	private String storage = "";

	@Override
	public String getType() {
		return "dispenser";
	}

	@Override
	public String[] getMethodNames() {
		return new String[] { "t", "e" };
	}

	@Override
	public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception
	{
		// Should do error checking, but Im testing
		if(method == 0 )
		{
			this.storage = (String) arguments[0];
			return new Object[] { arguments[0] };
		}
		if( method == 1 )
		{
			
			return new Object[] { this.storage };
		}
		return null;
	}

	@Override
	public boolean canAttachToSide(int side)
	{
		return true;
	}

	@Override
	public void attach(IComputerAccess computer)
	{
	}

	@Override
	public void detach(IComputerAccess computer)
	{
	}

	@Override
	public void update()
	{
	}

	@Override
	public void readFromNBT(NBTTagCompound nbt)
	{
		System.out.println("READING");
		this.storage = nbt.getString("DATA");
	}

	@Override
	public void writeToNBT(NBTTagCompound nbt)
	{
		System.out.println("WRITING");
		nbt.setString("DATA", this.storage);
	}
}

As you can see I have put up some test prints here and there. But the main problem is, I never see 'READING' or 'WRITING' getting called, so I cannot save my first method's string.

How to reproduce you ask?
Well, here are some steps:
  1. Take this code and run minecraft with it
  2. Place a computer next to a dispenser
  3. wrap to the dispenser and call the method: wrap.t("Some random string")
  4. call the following method: wrap.e() -> you should see 'Some random string' (this part works)
  5. Place another dispenser next to the computer (You dont have to wrap to it)
  6. call again the method: wrap.e() -> you should see 'Some random string', but it shows an empty string instead.
Im hoping it is a problem on my end, otherwise it definitely is a bug!
Thanks for reading!
theoriginalbit #2
Posted 19 January 2014 - 07:46 PM
Isn't read/write from NBT only invoked when the world is saving (every 5 minutes) or the internal server is closing/opening?
Engineer #3
Posted 19 January 2014 - 07:56 PM
Isn't read/write from NBT only invoked when the world is saving (every 5 minutes) or the internal server is closing/opening?
Very true, but even that does not do it. I also forgot to mention in the OP that I figured out when you place a dispenser down, cc creates a new instance instance of the IHostedPeripheral for each dispenser, even the ones which were placed before that.

I think I can hotfix it on my side, because I can get the coords of the block. But that really seems something CC should do.

So, I can Nordic it, but is this expected behaviour with all the new instances and stuff?
That doesn't leave the 'bug' where you can't use nbt data.
Engineer #4
Posted 20 January 2014 - 09:57 AM
I asked Dan200 on twitter, because since when do the developers look here? :P/>
Anyway, here is his respone: https://twitter.com/DanTwoHundred/status/425238078534348800

So lets play the waiting game!