Jump to content

Syncing Client/Server Gui Data in Container


Mattizin

Recommended Posts

Hi,

 

im currently trying to sync a forge Energy Value between the server and client for gui porpuses.

I got the tip to do it like the Vanilla Furnace to cache the value in the container and override detectandsendChanges and updateProgressBar

I think im missing something obvious, because detectAndSendChanges doesnt even get called :/

This are the aprts of my container:

 

public class ContainerCharger extends Container
{
private TileEntityCharger te;	
private int energy;

public ContainerCharger(IInventory playerInventory, TileEntityCharger te)
{
	this.te = te;

	// This container references items out of our own inventory (the 9 slots we hold ourselves)
        // as well as the slots from the player inventory so that the user can transfer items between
        // both inventories. The two calls below make sure that slots are defined for both inventories.
        addOwnSlots();
        addPlayerSlots(playerInventory);
}

@Override
public void detectAndSendChanges()
{
	super.detectAndSendChanges();

	for(int i = 0; i < this.listeners.size(); i++)
	{
		IContainerListener icontainerlistener = (IContainerListener)this.listeners.get(i);

		if(this.energy != te.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored())
		{
			icontainerlistener.sendProgressBarUpdate(this, this.energy, 
                                this.te.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored());
		}
	}		
	this.energy = this.te.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored();
}

@SideOnly(Side.CLIENT)
    public void updateProgressBar(int id, int data)
    {
        ((BaseEnergyStorage)this.te.getCapability(CapabilityEnergy.ENERGY, null)).setEnergyStored(data);
    }

Link to comment
Share on other sites

Sorry for not giving all inormation, i dont really know what you all need for this:

GuiHandler:

public class GuiHandler implements IGuiHandler
{
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) 
{
	BlockPos pos = new BlockPos(x, y, z);
	switch(ID)
	{
		case 1:
		{
			TileEntity te = world.getTileEntity(pos);
	        if (te instanceof TileEntityCharger) 
	        {
	            return new ContainerCharger(player.inventory, (TileEntityCharger) te);
	        }
		}
		default:
		{
			return null;
		}
	}
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
	BlockPos pos = new BlockPos(x, y, z);
	switch(ID)
	{
	case 1:
	{
		TileEntity te = world.getTileEntity(pos);
        if (te instanceof TileEntityCharger) 
        {
        	TileEntityCharger containerTileEntity = (TileEntityCharger) te;
            return new GuiCharger(containerTileEntity, new ContainerCharger(player.inventory, containerTileEntity));
        }
	}
	default:
	{
		return null;
	}
}
}
}

 

and the part of the BlockCharger:

 

public static final int GUI_ID = 1;

.....


@Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack heldItem, EnumFacing side,
                float hitX, float hitY, float hitZ) 
{
        // Only execute on the server
        if (world.isRemote)
        {
            return true;
        }
        TileEntity te = world.getTileEntity(pos);
        if (!(te instanceof TileEntityCharger)) 
        {
            return false;
        }
        player.openGui(MattechBiogas.instance, GUI_ID, world, pos.getX(), pos.getY(), pos.getZ());
        return true;
    }

Link to comment
Share on other sites

Here you go:

@Mod(modid = MattechBiogas.MODID, name = MattechBiogas.NAME, version = MattechBiogas.VERSION)
public class MattechBiogas
{
    public static final String MODID = "mattechbiogas";
    public static final String NAME = "Mattech Biogas";
    public static final String VERSION = "0.1";
    
    @Instance
    public static MattechBiogas instance = new MattechBiogas();
    
    @SidedProxy(clientSide="mattizin.mattechbiogas.proxy.ClientProxy", serverSide="mattizin.mattechbiogas.proxy.ServerProxy")
    public static CommonProxy proxy;
    
    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
    	proxy.preInit(event);
    }
    
    @EventHandler
    public void init(FMLInitializationEvent event)
    {
    	proxy.init(event);
    }
    
    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
    	proxy.postInit(event);
    }
}

 

public class BlockCharger extends BlockMattech implements ITileEntityProvider
{
public static final PropertyDirection FACING = PropertyDirection.create("facing");
public static final int GUI_ID = 1;

public BlockCharger()
{
	super(Material.IRON, "blockCharger");
	setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}


@Override
    public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) 
{
        world.setBlockState(pos, state.withProperty(FACING, FacingUtil.getFacingFromEntity(pos, placer)), 2);
    }

@Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack heldItem, EnumFacing side,
                float hitX, float hitY, float hitZ) 
{
        // Only execute on the server
        if (world.isRemote)
        {
            return true;
        }
        TileEntity te = world.getTileEntity(pos);
        if (!(te instanceof TileEntityCharger)) 
        {
            return false;
        }
        player.openGui(MattechBiogas.instance, GUI_ID, world, pos.getX(), pos.getY(), pos.getZ());
        return true;
    }

@Override
    public IBlockState getStateFromMeta(int meta) 
{
        return getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7));
    }

    @Override
    public int getMetaFromState(IBlockState state) 
    {
        return state.getValue(FACING).getIndex();
    }

    @Override
    protected BlockStateContainer createBlockState() 
    {
        return new BlockStateContainer(this, FACING);
    }

//----START ITileEntityProvider---//
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) 
{
		return new TileEntityCharger();
}
//----END ITileEntityProvider---//
}

Link to comment
Share on other sites

i thought that is the purpose of proxys :o

Here you go:

public class CommonProxy 
{
public void preInit(FMLPreInitializationEvent event)
{
	ModBlocks.registerBlocks();
	ModItems.registerItems();
	ModTileEntities.registerTileEntities();
}

    public void init(FMLInitializationEvent event)
    {
    	NetworkRegistry.INSTANCE.registerGuiHandler(MattechBiogas.instance, new GuiHandler());
    }
    
    public void postInit(FMLPostInitializationEvent event)
    {
    	
    }
}

 

public class ClientProxy extends CommonProxy
{
@Override
public void preInit(FMLPreInitializationEvent event) 
{
	super.preInit(event); 

	MattechBiogasModelManager.registerBlockModels();
}

@Override
public void init(FMLInitializationEvent event) 
{
	super.init(event);
}

@Override
public void postInit(FMLPostInitializationEvent event) 
{
	super.postInit(event);
}
}

 

public class ServerProxy extends CommonProxy 
{
@Override
public void preInit(FMLPreInitializationEvent event) 
{
	super.preInit(event);
}

@Override
public void init(FMLInitializationEvent event) 
{
	super.init(event);
}

@Override
public void postInit(FMLPostInitializationEvent event) 
{
	super.postInit(event);
}
}

 

 

 

Does not implementing ITileEntityProvider change anything or is it just style wise?

Link to comment
Share on other sites

i thought that is the purpose of proxys :o

 

No, the purpose of proxies is to allow for code that only exists on one side to have a place to live.

 

Predominantly you need:

CommonProxy: contains empty stub methods that do nothing

ClientProxy: contains all client-side-only code

 

The "server proxy" has no reason for existing at all because anything you might ever want to do there must happen on the client during SSP which in your proxy setup, won't happen.

 

Does not implementing ITileEntityProvider change anything or is it just style wise?

 

The methods that ITileEntityProvider declares are already declared in the Block class.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

This is my whole Container:

 

public class ContainerCharger extends Container
{
private TileEntityCharger te;	
private int energy;

public ContainerCharger(IInventory playerInventory, TileEntityCharger te)
{
	this.te = te;

	// This container references items out of our own inventory (the 9 slots we hold ourselves)
        // as well as the slots from the player inventory so that the user can transfer items between
        // both inventories. The two calls below make sure that slots are defined for both inventories.
        addOwnSlots();
        addPlayerSlots(playerInventory);
}

@Override
public void detectAndSendChanges()
{
	super.detectAndSendChanges();

	for(int i = 0; i < this.listeners.size(); i++)
	{
		IContainerListener icontainerlistener = (IContainerListener)this.listeners.get(i);

		if(this.energy != te.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored())
		{
			icontainerlistener.sendProgressBarUpdate(this, this.energy, this.te.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored());
		}
	}		
	this.energy = this.te.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored();
}

@SideOnly(Side.CLIENT)
    public void updateProgressBar(int id, int data)
    {
        ((BaseEnergyStorage)this.te.getCapability(CapabilityEnergy.ENERGY, null)).setEnergyStored(data);
    }

private void addOwnSlots()
{
        IItemHandler itemHandler = this.te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

        addSlotToContainer(new SlotItemHandler(itemHandler, 0, 64, 34));
        addSlotToContainer(new SlotOutput(itemHandler, 1, 100, 34));
    }

    private void addPlayerSlots(IInventory playerInventory) 
    {
        // Slots for the main inventory
        for (int row = 0; row < 3; ++row) 
        {
            for (int col = 0; col < 9; ++col) 
            {
                int x = 10 + col * 18;
                int y = row * 18 + 70;
                this.addSlotToContainer(new Slot(playerInventory, col + row * 9 + 10, x, y));
            }
        }
        // Slots for the hotbar
        for (int row = 0; row < 9; ++row)
        {
            int x = 10 + row * 18;
            int y = 58 + 70;
            this.addSlotToContainer(new Slot(playerInventory, row, x, y));
        }
    }	
    
    @Nullable
    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) 
    {
        ItemStack itemstack = null;
        Slot slot = this.inventorySlots.get(index);

        if (slot != null && slot.getHasStack()) 
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (index < TileEntityCharger.SIZE) 
            {
                if (!this.mergeItemStack(itemstack1, TileEntityCharger.SIZE, this.inventorySlots.size(), true)) 
                {
                    return null;
                }
            } 
            else if (!this.mergeItemStack(itemstack1, 0, TileEntityCharger.SIZE, false)) 
            {
                return null;
            }
            if (itemstack1.stackSize == 0) 
            {
                slot.putStack(null);
            } else 
            {
                slot.onSlotChanged();
            }
        }
        return itemstack;
    }
    
@Override
public boolean canInteractWith(EntityPlayer playerIn) 
{
	return te.canInteractWith(playerIn);
}
}

 

the canInteractwith returns te.canInteractWith which is:

public boolean canInteractWith(EntityPlayer playerIn) 
{
    // If we are too far away from this tile entity you cannot use it
	return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
}

Link to comment
Share on other sites

Ok i solved it with just rewriting sendanddetectchanges and sendprogressbarUpdate from scratch.

Only thing i changed is to set the parameter "value to change" to 0 instead of the actual field that has to be changed.

I dont like the way vanilly does the progressbar things with hardcoded integers that describe the field of the te. Why do i want to do sth like this?

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.

×
×
  • Create New...

Important Information

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