Jump to content

[1.10.2] TileEntitys Data synchronisation and updating Containers


Roxane

Recommended Posts

I'm trying to update my mod from 1.9 to 1.10.2 (forge version 12.16.1.1887 to 12.18.2.2098).

 

Most of my mods work just fine, but with my AccessControl I'm having a little bit of a problem. The access panels can have different colors and some other properties, but on loading a world they all have the default properties on the client side. It seems that on the server side they have correct properties because they are behaving as they should, although looking differently. As soon as they are activated once the clientside gets the correct data and now they are looking as they should.

 

This is my code of the BlockAccessPanel.java

 

package net.roxa.accessControl;

import java.util.List;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.scoreboard.Team;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class BlockAccessPanel extends BlockContainer
{
//TileEntityAccessPanel tileEntity;

    public static final PropertyBool POWERED = PropertyBool.create("powered");
    public static final PropertyDirection FACING = PropertyDirection.create("facing");
    public static final PropertyEnum COLOR = PropertyEnum.create("color", EnumColor.class);

public BlockAccessPanel() {
	super(Material.CIRCUITS);
	this.setDefaultState(this.blockState.getBaseState().withProperty(POWERED, Boolean.valueOf(false)).withProperty(COLOR, EnumColor.WHITE).withProperty(FACING, EnumFacing.UP));
	this.setUnlocalizedName("accessPanel");
	this.setRegistryName("accessPanel");
	this.setCreativeTab(CreativeTabs.REDSTONE);
	this.setSoundType(SoundType.METAL);
	this.setHardness(0.5F);
	this.setResistance(1F);
	//this.useNeighborBrightness = true;

	//tileEntity = new TileEntityAccessPanel();
}

@SideOnly(Side.CLIENT)
public BlockRenderLayer getBlockLayer()
{
	return BlockRenderLayer.CUTOUT_MIPPED;
}

@Override
public boolean isOpaqueCube(IBlockState state)
{
	return false;
}

@Override
public boolean isFullCube(IBlockState state)
{
	return false;
}

@Override
public EnumBlockRenderType getRenderType(IBlockState state)
{
	return EnumBlockRenderType.MODEL;
}

/*
@Override
public void setBlockBoundsForItemRender()
    {
        float f = 0.1875F;
        float f1 = 0.125F;
        float f2 = 0.125F;
        this.setBlockBounds(0.5F - f, 0.5F - f1, 0.5F - f2, 0.5F + f, 0.5F + f1, 0.5F + f2);
    }
    */

@Override
public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side)
    {
        return worldIn.isSideSolid(pos.offset(side.getOpposite()), side, true);
    }

@Override
public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
    {
        EnumFacing[] aenumfacing = EnumFacing.values();
        int i = aenumfacing.length;

        for (int j = 0; j < i; ++j)
        {
            EnumFacing enumfacing = aenumfacing[j];

            if (worldIn.isSideSolid(pos.offset(enumfacing), enumfacing.getOpposite(), true))
            {
                return true;
            }
        }

        return false;
    }

@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
    {
        return worldIn.isSideSolid(pos.offset(facing.getOpposite()), facing, true) ? this.getDefaultState().withProperty(FACING, facing).withProperty(POWERED, Boolean.valueOf(false)) : this.getDefaultState().withProperty(FACING, EnumFacing.DOWN).withProperty(POWERED, Boolean.valueOf(false));
    }

@Override
public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block neighborBlock)
    {
        if (this.checkForDrop(worldIn, pos, state))
        {
            EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);

            if (!worldIn.isSideSolid(pos.offset(enumfacing.getOpposite()), enumfacing, true))
            {
                this.dropBlockAsItem(worldIn, pos, state, 0);
                worldIn.setBlockToAir(pos);
            }
        }
    }

    private boolean checkForDrop(World worldIn, BlockPos pos, IBlockState state)
    {
        if (!this.canPlaceBlockAt(worldIn, pos))
        {
            this.dropBlockAsItem(worldIn, pos, state, 0);
            worldIn.setBlockToAir(pos);
            return false;
        }
        else
        {
            return true;
        }
    }
    
    @Override
    //public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
    public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
    {
        return this.updateBlockBounds(source.getBlockState(pos));
    }

    private AxisAlignedBB updateBlockBounds(IBlockState state)
    {
    	if (state.getBlock() instanceof BlockAccessPanel)
    	{
    	AxisAlignedBB axis;
        EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
        float f = 0.25F;
        float f1 = 0.375F;
        float f2 = 1 / 16.0F;
        float f3 = 0.1875F;
        float f4 = 1 - 0.1875F;

        switch (BlockAccessPanel.SwitchEnumFacing.FACING_LOOKUP[enumfacing.ordinal()])
        {
            case 1:
            	axis = new AxisAlignedBB(0.0F, f3, f3, f2, f4, f4);
                break;
            case 2:
            	axis = new AxisAlignedBB(1.0F - f2, f3, f3, 1.0F, f4, f4);
                break;
            case 3:
            	axis = new AxisAlignedBB(f3, f3, 0.0F, f4, f4, f2);
                break;
            case 4:
            	axis = new AxisAlignedBB(f3, f3, 1.0F - f2, f4, f4, 1.0F);
                break;
            case 5:
            	axis = new AxisAlignedBB(f3, 0.0F, f3, f4, 0.0F + f2, f4);
                break;
            default:
            	axis = new AxisAlignedBB(f3, 1.0F - f2, f3, f4, 1.0F, f4);
        }
        return axis;
    	}
    	else return null;
    }
    
    /*
    @Override
    public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
    {
        return null;
    }
    */

@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
    {
        if (((Boolean)state.getValue(POWERED)).booleanValue())
        {
            this.notifyNeighbors(worldIn, pos, (EnumFacing)state.getValue(FACING));
        }

        super.breakBlock(worldIn, pos, state);
    }

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)
    {
	boolean activated = false;
	boolean canAccess = false;
    	//ItemStack itemStackHold = playerIn.getHeldItem();
    	Item item;
    	if (heldItem != null) item = heldItem.getItem();
    	else item = null;
    	if (item == RoxaAccessControlMod.accessCard && !playerIn.isSneaking())
    	{
    		TileEntity tileEntity = worldIn.getTileEntity(pos);
    		TileEntityAccessPanel entityAccessPanel = null;
    		if (tileEntity instanceof TileEntityAccessPanel) entityAccessPanel = (TileEntityAccessPanel)tileEntity;
    		ItemAccessCard card = (ItemAccessCard) item;
    		
    		//Testing for the right color
            if (card.canAccess(heldItem, (entityAccessPanel.getColor())))
            {
            	//activated = activateBlock(worldIn, state, pos);
            	canAccess = true;
            	//System.out.println("Access because of color saved on Card");
            }
            //Testing for Panels on Card or Player on Panel
            else if (entityAccessPanel != null)
    		{
    			String[] playerNames = null;
    			playerNames = entityAccessPanel.getPlayerNames();
    			String playerName = playerIn.getDisplayNameString();
    			if (playerNames != null)
    			{
    				for (int i = 0; i < playerNames.length; i++)
                    {
        				if (playerNames[i].equals(playerName))
                    	{
                    		//activated = activateBlock(worldIn, state, pos);
        					canAccess = true;
                    		//System.out.println("Access because of player saved in Panel");
                    	}
        				if (playerNames[i].equals(""))
                    	{
                    		canAccess = true;
                    		//System.out.println("Access because everybody has Access");
                    	}
                    }
    			}
    			
    			if (!canAccess && card.getPanels(heldItem) != null)
    			{
    				String[] panels = card.getPanels(heldItem);
    				String panelName = entityAccessPanel.getPanelName();
    				for (int i = 0; i < panels.length; i++)
                    {
    					if (panels[i].equals(panelName))
    					{
    						canAccess = true;
    						//activated = activateBlock(worldIn, state, pos);
    						//System.out.println("Access because of panel name is on Card");
    					}
                    }
    			}
    			
    			if (!canAccess)
    			{
    				worldIn.playSound(null, pos, RoxaAccessControlMod.soundDenied, SoundCategory.BLOCKS, 0.6F, 1.0F);
    			}
    		}
    		
    		if (((Boolean)state.getValue(POWERED)).booleanValue())
            {
    			if(!entityAccessPanel.isToggle()) activated = true;
    			else if (canAccess)
    			{
    				deactivateBlock(worldIn, state, pos);
    				entityAccessPanel.incCounter();
        			activateOtherPanels(worldIn, pos, entityAccessPanel, false);
    				worldIn.playSound(null, pos, RoxaAccessControlMod.soundGranted, SoundCategory.BLOCKS, 0.4F, 1.0F);
    			}
            }
    		else if (canAccess)
    		{
			activateBlock(worldIn, state, pos, entityAccessPanel.isToggle());
    			entityAccessPanel.incCounter();
    			activateOtherPanels(worldIn, pos, entityAccessPanel, true);
    			worldIn.playSound(null, pos, RoxaAccessControlMod.soundGranted, SoundCategory.BLOCKS, 0.6F, 1.0F);
    		}
    	}
    	else
    	{
    		//Do nothing
    	}
    	return activated;
    }

private boolean activateBlock(World worldIn, IBlockState state, BlockPos pos, boolean toggle)
{
	worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(true)), 3);
        worldIn.markBlockRangeForRenderUpdate(pos, pos);
        worldIn.playSound(null, pos, SoundEvents.BLOCK_STONE_BUTTON_CLICK_ON, SoundCategory.BLOCKS, 0.3F, 0.6F);
        this.notifyNeighbors(worldIn, pos, (EnumFacing)state.getValue(FACING));
        if(!toggle) worldIn.scheduleUpdate(pos, this, 40);
        return true;
}

public void deactivateBlock(World worldIn, IBlockState state, BlockPos pos)
{
	if (!worldIn.isRemote)
        {
            if (((Boolean)state.getValue(POWERED)).booleanValue())
            {
                worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(false)));
                this.notifyNeighbors(worldIn, pos, (EnumFacing)state.getValue(FACING));
                worldIn.playSound(null, pos, SoundEvents.BLOCK_STONE_BUTTON_CLICK_OFF, SoundCategory.BLOCKS, 0.3F, 0.5F);
                worldIn.markBlockRangeForRenderUpdate(pos, pos);
            }
        }
}

public void activateOtherPanels(World worldIn, BlockPos pos, TileEntityAccessPanel entityAccessPanel, boolean activate)
{
	String panelName = entityAccessPanel.getPanelName();
	if (!panelName.isEmpty())
	{
		for(int i = -5; i <= 5; i++)
		{
			for(int j = -5; j <= 5; j++)
			{
				BlockPos posOther = pos.add(i, 0, j);
				Block blockOther = worldIn.getBlockState(posOther).getBlock();
				if(blockOther == RoxaAccessControlMod.accessPanel && (i != 0 | j != 0))
				{
					TileEntity tileEntityOther = worldIn.getTileEntity(posOther);
		    		TileEntityAccessPanel entityAccessPanelOther = null;
		    		if (tileEntityOther instanceof TileEntityAccessPanel) entityAccessPanelOther = (TileEntityAccessPanel)tileEntityOther;
		    		String panelNameOther = entityAccessPanelOther.getPanelName();
		    		if(panelName.equals(panelNameOther))
		    		{
		    			BlockAccessPanel blockPanel = (BlockAccessPanel) blockOther;
		    			if(activate)
		    			{
		    				blockPanel.activateBlock(worldIn, worldIn.getBlockState(posOther), posOther, entityAccessPanelOther.isToggle());
		    				//System.out.println("Activating Access Panel at " + i + " " + j + " with same name");
		    				//System.out.println("Counter is: " + entityAccessPanel.getCounter());
	    				}
		    			else 
		    			{
		    				blockPanel.deactivateBlock(worldIn, worldIn.getBlockState(posOther), posOther);
		    				//System.out.println("Deaktivating Access Panel at " + i + " " + j + " with same name");
		    				//System.out.println("Counter is: " + entityAccessPanel.getCounter());
		    			}
		    			if (entityAccessPanel.getCounter() != entityAccessPanelOther.getCounter())
		    			{
		    				entityAccessPanelOther.setCounter(entityAccessPanel.getCounter());
		    				activateOtherPanels(worldIn, posOther, entityAccessPanelOther, activate);
		    			}
		    		}
				}
			}
		}
		for(int x = -3; x <= 3; x++)
		{
			for(int y = -3; y <= 3; y++)
			{
				if (y!=0)
				{
					for(int z = -3; z <= 3; z++)
					{
						BlockPos posOther = pos.add(x, y, z);
						Block blockOther = worldIn.getBlockState(posOther).getBlock();
						if(blockOther == RoxaAccessControlMod.accessPanel)
						{
							TileEntity tileEntityOther = worldIn.getTileEntity(posOther);
				    		TileEntityAccessPanel entityAccessPanelOther = null;
				    		if (tileEntityOther instanceof TileEntityAccessPanel) entityAccessPanelOther = (TileEntityAccessPanel)tileEntityOther;
				    		String panelNameOther = entityAccessPanelOther.getPanelName();
				    		if(panelName.equals(panelNameOther))
				    		{
				    			BlockAccessPanel blockPanel = (BlockAccessPanel) blockOther;
				    			if(activate)
				    			{
				    				blockPanel.activateBlock(worldIn, worldIn.getBlockState(posOther), posOther, entityAccessPanelOther.isToggle());
				    				//System.out.println("Activating Access Panel at " + i + " " + j + " with same name");
				    				//System.out.println("Counter is: " + entityAccessPanel.getCounter());
			    				}
				    			else 
				    			{
				    				blockPanel.deactivateBlock(worldIn, worldIn.getBlockState(posOther), posOther);
				    				//System.out.println("Deaktivating Access Panel at " + i + " " + j + " with same name");
				    				//System.out.println("Counter is: " + entityAccessPanel.getCounter());
				    			}
				    			if (entityAccessPanel.getCounter() != entityAccessPanelOther.getCounter())
				    			{
				    				entityAccessPanelOther.setCounter(entityAccessPanel.getCounter());
				    				activateOtherPanels(worldIn, posOther, entityAccessPanelOther, activate);
				    			}
				    		}
						}
					}
				}
			}
		}
	}
}

@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
    {
        this.deactivateBlock(worldIn, state, pos);
        TileEntityAccessPanel entityAccessPanel = (TileEntityAccessPanel) worldIn.getTileEntity(pos);
        entityAccessPanel.incCounter();
        activateOtherPanels(worldIn, pos, entityAccessPanel, false);
    }

public int isProvidingWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
    {
        return ((Boolean)state.getValue(POWERED)).booleanValue() ? 15 : 0;
    }

@Override
    public int getStrongPower(IBlockState state, IBlockAccess worldIn, BlockPos pos, EnumFacing side)
    {
	return !((Boolean)state.getValue(POWERED)).booleanValue() ? 0 : (state.getValue(FACING) == side ? 15 : 0);
    }

@Override
    public boolean canProvidePower(IBlockState state)
    {
	return true;
    }
    
    private void notifyNeighbors(World worldIn, BlockPos pos, EnumFacing facing)
    {
        worldIn.notifyNeighborsOfStateChange(pos, this);
        worldIn.notifyNeighborsOfStateChange(pos.offset(facing.getOpposite()), this);
    }
    
    public IBlockState getStateFromMeta(int meta)
    {
        EnumFacing enumfacing;

        switch (meta & 7)
        {
            case 0:
                enumfacing = EnumFacing.DOWN;
                break;
            case 1:
                enumfacing = EnumFacing.EAST;
                break;
            case 2:
                enumfacing = EnumFacing.WEST;
                break;
            case 3:
                enumfacing = EnumFacing.SOUTH;
                break;
            case 4:
                enumfacing = EnumFacing.NORTH;
                break;
            case 5:
            default:
                enumfacing = EnumFacing.UP;
        }
        return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(POWERED, Boolean.valueOf((meta &  > 0));
    }

    public int getMetaFromState(IBlockState state)
    {
        int i;

        switch (BlockAccessPanel.SwitchEnumFacing.FACING_LOOKUP[((EnumFacing)state.getValue(FACING)).ordinal()])
        {
            case 1:
                i = 1;
                break;
            case 2:
                i = 2;
                break;
            case 3:
                i = 3;
                break;
            case 4:
                i = 4;
                break;
            case 5:
            default:
                i = 5;
                break;
            case 6:
                i = 0;
        }

        if (((Boolean)state.getValue(POWERED)).booleanValue())
        {
            i |= 8;
        }

        return i;
    }
    
    @Override
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
    {
    	TileEntityAccessPanel teap = (TileEntityAccessPanel) worldIn.getTileEntity(pos);
    	return worldIn.getBlockState(pos).withProperty(COLOR, this.getPanelColor(teap.getColor()));
    }
    
    @Override
    protected BlockStateContainer createBlockState()
    {
    	return new BlockStateContainer(this, new IProperty[] {POWERED, FACING, COLOR});
    }

@Override
public TileEntity createNewTileEntity(World worldIn, int meta)
{
	return new TileEntityAccessPanel();
}

public EnumColor getPanelColor(int colorIn)
{
	EnumColor color = null;
	switch (colorIn) 
	{
	case 0:
		color = EnumColor.WHITE;
		break;
	case 1:
		color = EnumColor.YELLOW;
		break;
	case 2:
		color = EnumColor.ORANGE;
		break;
	case 3:
		color = EnumColor.RED;
		break;
	case 4:
		color = EnumColor.PURPLE;
		break;
	case 5:
		color = EnumColor.BLUE;
		break;
	case 6:
		color = EnumColor.GREEN;
		break;
	case 7:
		color = EnumColor.BLACK;
		break;
	case 8:
		color = EnumColor.GREY;
		break;
	case 9:
		color = EnumColor.LIGHT_GREY;
		break;
	case 10:
		color = EnumColor.PINK;
		break;
	case 11:
		color = EnumColor.MAGENTA;
		break;
	case 12:
		color = EnumColor.LIGHT_BLUE;
		break;
	case 13:
		color = EnumColor.CYAN;
		break;
	case 14:
		color = EnumColor.LIME;
		break;
	case 15:
		color = EnumColor.BROWN;
		break;
	}
	return color;
}

public void setPanelColor(World worldIn, BlockPos posIn, int colorIn)
{
	TileEntityAccessPanel teap = (TileEntityAccessPanel) worldIn.getTileEntity(posIn);
	teap.setColor(colorIn);
	IBlockState state = worldIn.getBlockState(posIn);
	switch (colorIn) 
	{
	case 0:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.WHITE));
		break;
	case 1:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.YELLOW));
		break;
	case 2:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.ORANGE));
		break;
	case 3:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.RED));
		break;
	case 4:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.PURPLE));
		break;
	case 5:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.BLUE));
		break;
	case 6:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.GREEN));
		break;
	case 7:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.BLACK));
		break;
	case 8:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.GREY));
		break;
	case 9:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.LIGHT_GREY));
		break;
	case 10:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.PINK));
		break;
	case 11:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.MAGENTA));
		break;
	case 12:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.LIGHT_BLUE));
		break;
	case 13:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.CYAN));
		break;
	case 14:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.LIME));
		break;
	case 15:
		worldIn.setBlockState(posIn, state.withProperty(COLOR, EnumColor.BROWN));
		break;
	}
	//System.out.println(worldIn.getBlockState(posIn));
	//TODO Mit Mehrspieler testen: Tut das hier das, was es soll?
	//worldIn.markBlockForUpdate(posIn);
	worldIn.markBlockRangeForRenderUpdate(posIn, posIn);
}

static final class SwitchEnumFacing
    {
        static final int[] FACING_LOOKUP = new int[EnumFacing.values().length];
        private static final String __OBFID = "CL_00002131";

        static
        {
            try
            {
                FACING_LOOKUP[EnumFacing.EAST.ordinal()] = 1;
            }
            catch (NoSuchFieldError var6)
            {
                ;
            }

            try
            {
                FACING_LOOKUP[EnumFacing.WEST.ordinal()] = 2;
            }
            catch (NoSuchFieldError var5)
            {
                ;
            }

            try
            {
                FACING_LOOKUP[EnumFacing.SOUTH.ordinal()] = 3;
            }
            catch (NoSuchFieldError var4)
            {
                ;
            }

            try
            {
                FACING_LOOKUP[EnumFacing.NORTH.ordinal()] = 4;
            }
            catch (NoSuchFieldError var3)
            {
                ;
            }

            try
            {
                FACING_LOOKUP[EnumFacing.UP.ordinal()] = 5;
            }
            catch (NoSuchFieldError var2)
            {
                ;
            }

            try
            {
                FACING_LOOKUP[EnumFacing.DOWN.ordinal()] = 6;
            }
            catch (NoSuchFieldError var1)
            {
                ;
            }
        }
    }
}

and this is the TileEntityAccessPanel.java

 

package net.roxa.accessControl;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagByte;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class TileEntityAccessPanel extends TileEntity
{
String name = "";
boolean toggleMode;
String players[];
int color = 0;
private int counter = 0;

public TileEntityAccessPanel()
{

}

@Override
public void readFromNBT(NBTTagCompound tagCompound)
{
	super.readFromNBT(tagCompound);

	name = tagCompound.getString("Name");
	if (name == null) name = "";

	toggleMode = tagCompound.getBoolean("Toggle");

	color = tagCompound.getInteger("Color");

	NBTTagCompound tag = tagCompound.getCompoundTag("Player");
  		
  		if (tag != null)
  		{
  			int playerAmount = 0;
  	  		for (int i = 0; tag.hasKey("Player" + i); i++)
  			{
  	  		playerAmount++;
  			}
  	  		
  	  		players = new String[playerAmount];
  	  		
  	  		for (int i = 0; i < players.length; i++)
  			{
  	  		players[i] = tag.getString("Player" + i);
  			}

  	  		if (playerAmount == 0) players = null;
  		}
  		else
  		{
  			players = null;
  		}
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound tagCompound)
{
	super.writeToNBT(tagCompound);

	tagCompound.setString("Name", name);

	tagCompound.setBoolean("Toggle", toggleMode);

	tagCompound.setInteger("Color", color);

	NBTTagCompound tag = new NBTTagCompound();
    	
    	if (players != null)
    	{
    		NBTTagString string = new NBTTagString();
    		for (int i = 0; i < players.length; i++)
    		{
    			string = new NBTTagString(players[i]);
    			tag.setTag("Player" + i, string);
    		}
    		tagCompound.setTag("Player", tag);
    	}
    	else tagCompound.setTag("Player", tag);
	return tagCompound;

}

@Override
public SPacketUpdateTileEntity getUpdatePacket()
{
	NBTTagCompound syncData = new NBTTagCompound();
	this.writeToNBT(syncData);
	return new SPacketUpdateTileEntity(pos, 0, syncData);
}

@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
{
	this.readFromNBT(pkt.getNbtCompound());
	RoxaAccessControlMod.network.sendToServer(new MessageInt(MessageInt.requestTileEntityForPanel, pos));
}

public void addPlayer(String nameIn)
  	{
  		int newLength;
  		if (players == null)
  		{
  			//System.out.println("Read no Players from NBT. Creating new String[]");
  			newLength = 1;
  			players = new String[newLength];
  			players[0] = nameIn;
  		}
  		else 
  		{
  			//System.out.println("Read some Players from NBT");
  			newLength = players.length + 1;
  			String[] old = players;
  			players = new String[newLength];
  			for (int i = 0; i < old.length; i++)
  			{
  				players[i] = old[i];
  				//System.out.println("Player[" + i + "] is now: " + players[i]);
  			}
  			players[newLength - 1] = nameIn;
  		}
  	}

public boolean doesPlayerExist(String nameIn)
{
	boolean exists = false;
	for (int i = 0; players != null && i < players.length; i++)
	{
		if (nameIn.equals(players[i])) exists = true;
	}
	return exists;
}

public void deletePlayer(int index)
{
	String[] old = players;

	RoxaAccessControlMod.network.sendToServer(new MessageInt(MessageInt.deletePlayerFromPanel, index));

	if (players != null && players.length > 1)
  		{
		players = new String[old.length - 1];
  			for (int i = 0; i < index; i++)
  			{
  				players[i] = old[i];
  			}
  			for (int i = players.length - 1; i >= index; i--)
  			{
  				players[i] = old[i+1];
  			}
  		}
  		else
  		{
  			players = null;
  		}
}

public boolean isToggle()
{
	return toggleMode;
}

public void setToggle(boolean toggleMode)
{
	this.toggleMode = toggleMode;

}

public void setToggleOtherPanels(World worldIn, BlockPos pos, boolean toggle)
{
	if (!name.isEmpty())
	{
		for(int i = -5; i <= 5; i++)
		{
			for(int j = -5; j <= 5; j++)
			{
				BlockPos posOther = pos.add(i, 0, j);
				Block blockOther = worldIn.getBlockState(posOther).getBlock();
				if(blockOther == RoxaAccessControlMod.accessPanel && (i != 0 | j != 0))
				{
					TileEntity tileEntityOther = worldIn.getTileEntity(posOther);
		    		TileEntityAccessPanel entityAccessPanelOther = null;
		    		if (tileEntityOther instanceof TileEntityAccessPanel) entityAccessPanelOther = (TileEntityAccessPanel)tileEntityOther;
		    		String panelNameOther = entityAccessPanelOther.getPanelName();
		    		if(name.equals(panelNameOther))
		    		{
		    			//System.out.println("Setting Toggle Mode to " + toggle + " at " + i + " " + j);
		    			entityAccessPanelOther.setToggle(toggle);
		    			
		    			TileEntity tileEntity = worldIn.getTileEntity(pos);
			    		TileEntityAccessPanel entityAccessPanel = null;
			    		if (tileEntity instanceof TileEntityAccessPanel) entityAccessPanel = (TileEntityAccessPanel)tileEntity;
			    		//System.out.println("Counter is: " + entityAccessPanel.getCounter());
		    			if (entityAccessPanel.getCounter() != entityAccessPanelOther.getCounter())
		    			{
		    				entityAccessPanelOther.setCounter(entityAccessPanel.getCounter());
		    				setToggleOtherPanels(worldIn, posOther, toggle);
		    			}
		    		}
				}
			}
		}
		for(int x = -3; x <= 3; x++)
		{
			for(int y = -3; y <= 3; y++)
			{
				if (y!=0)
				{
					for(int z = -3; z <= 3; z++)
					{
						BlockPos posOther = pos.add(x, y, z);
						Block blockOther = worldIn.getBlockState(posOther).getBlock();
						if(blockOther == RoxaAccessControlMod.accessPanel)
						{
							TileEntity tileEntityOther = worldIn.getTileEntity(posOther);
				    		TileEntityAccessPanel entityAccessPanelOther = null;
				    		if (tileEntityOther instanceof TileEntityAccessPanel) entityAccessPanelOther = (TileEntityAccessPanel)tileEntityOther;
				    		String panelNameOther = entityAccessPanelOther.getPanelName();
				    		if(name.equals(panelNameOther))
				    		{
				    			System.out.println("Setting Toggle Mode to " + toggle + " at " + x + " " + y + " " + z);
				    			entityAccessPanelOther.setToggle(toggle);
				    			
				    			TileEntity tileEntity = worldIn.getTileEntity(pos);
					    		TileEntityAccessPanel entityAccessPanel = null;
					    		if (tileEntity instanceof TileEntityAccessPanel) entityAccessPanel = (TileEntityAccessPanel)tileEntity;
					    		System.out.println("Counter is: " + entityAccessPanel.getCounter());
				    			if (entityAccessPanel.getCounter() != entityAccessPanelOther.getCounter())
				    			{
				    				entityAccessPanelOther.setCounter(entityAccessPanel.getCounter());
				    				setToggleOtherPanels(worldIn, posOther, toggle);
				    			}
				    		}
						}
					}
				}
			}
		}
	}
}

public int getColor()
{
	return this.color;
}

public void setColor(int colorIn)
{
	color = colorIn;
}

public String[] getPlayerNames()
{
	return this.players;
}

public void setPlayers(String[] playerIn)
{
	players = playerIn;
}

public void setPanelName(String nameIn)
{
	name = nameIn;
}

public String getPanelName()
{
	return name;
}

public String getName()
{
	return "tileEntity.accessPanel";
}

public int getCounter()
{
	return counter;
}

public void setCounter(int counterIn)
{
	counter = counterIn;
}

public void incCounter()
{
	counter++;
}


@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate)
    {
        return (oldState.getBlock() != newSate.getBlock());
    }
}

 

 

and I can't seem to find a correct way to update this line

((ICrafting)this.crafters.get(j)).sendSlotContents(this, i, itemstack1);

in public void detectAndSendChanges() in the container...

ContainerAccessAdministrator.java:

 

package net.roxa.accessControl;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerWorkbench;
import net.minecraft.inventory.IContainerListener;
//import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class ContainerAccessAdministrator extends Container
{
public IInventory cardSlot;// = new InventoryCrafting(this, 1, 1); 
protected TileEntityAccessAdministrator tileEntity;

    private World worldObj;
    private BlockPos pos;
    private EntityPlayer player;

    public ContainerAccessAdministrator(EntityPlayer playerIn, InventoryPlayer playerInventory, World worldIn, BlockPos posIn, TileEntityAccessAdministrator te)
    {
        this.worldObj = worldIn;
        this.pos = posIn;
        this.player = playerIn;
        this.tileEntity = te;
        cardSlot = te;
        int i;
        int j;
        
        this.addSlotToContainer(new Slot(this.cardSlot, 0, 14, );//22, 25)); //Slot 0 of custom "Inventory"
        
        for (i = 0; i < 3; ++i)
        {
            for (j = 0; j < 9; ++j)
            {
                this.addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 48 + j * 18, 121 + i * 18)); //Slot (9 to 35) or (10 to 36)
            }
        }
        
        for (i = 0; i < 9; ++i)
        {
            this.addSlotToContainer(new Slot(playerInventory, i, 48 + i * 18, 179)); //Slot (0 to  or (1 to 9)
        }
        EntityPlayer player = playerInventory.player;
        if (!worldIn.isRemote)
        {
        	//System.out.println("Casting");
        	if (RoxaAccessControlMod.network != null) 
        	{
        		ItemStack stack = cardSlot.getStackInSlot(0);
        		if (stack != null && stack.getItem() == RoxaAccessControlMod.accessCard) 
        		{
        			ItemAccessCard card = (ItemAccessCard) stack.getItem();
        			String[] panels = card.getPanels(stack);
        			int length = 0;
        			if (panels != null) length = panels.length;
        			RoxaAccessControlMod.network.sendTo(new MessageInt(MessageInt.colorsToAdministratorGui, card.getColor(stack)), (EntityPlayerMP) player);//worldIn.getPlayerEntityByUUID(playerInventory.player.getUniqueID()));
        			RoxaAccessControlMod.network.sendTo(new MessageStringArray(MessageStringArray.panelsToAdministratorGui, panels, length), (EntityPlayerMP) player);
        		}
            	else 
            	{
            		RoxaAccessControlMod.network.sendTo(new MessageInt(MessageInt.colorsToAdministratorGui, 0), (EntityPlayerMP) player);//worldIn.getPlayerEntityByUUID(playerInventory.player.getUniqueID()));
            		RoxaAccessControlMod.network.sendTo(new MessageStringArray(MessageStringArray.panelsToAdministratorGui, null, 0), (EntityPlayerMP) player);
            	}
        	}
        }
    }
    
    @Override
    public boolean canInteractWith(EntityPlayer playerIn)
    {
        return playerIn.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
    }
    
    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
    {
    	ItemStack itemstack = null;
    	Slot slot = (Slot)this.inventorySlots.get(index);
    	if (slot != null && slot.getHasStack())
    	{
    		ItemStack itemstack1 = slot.getStack();
    		itemstack = itemstack1.copy();
    		// If itemstack is in Output stack
    		if (index == 0)
    		{
    			// try to place in player inventory / action bar; add 36+1 because mergeItemStack uses < index,
    			// so the last slot in the inventory won't get checked if you don't add 1
    			if (!this.mergeItemStack(itemstack1, 0+1, 0+36+1, true))
    			{
    				return null;
    			}
    			slot.onSlotChange(itemstack1, itemstack);
    		}
    		// itemstack is in player inventory, try to place in appropriate furnace slot
    		else if (index != 0)
    		{
    			//if it is an access Card, place it in the access card slot
    			if (itemstack1.getItem() == RoxaAccessControlMod.accessCard)
    			{
    				if (!this.mergeItemStack(itemstack1, 0, 0+1, false))
    				{
    					return null;
    				}
    			}
    			//item in player's inventory, but not in action bar
    			else if (index >= 0+1 && index < 0+28)
    			{
    				//place in action bar
    				if (!this.mergeItemStack(itemstack1, 0+28, 0+37, false))
    				{
    					return null;
    				}
    			}
    			//item in action bar - place in player inventory
    			else if (index >= 0+28 && index < 0+37 && !this.mergeItemStack(itemstack1, 0+1, 0+28, false))
    			{
    				return null;
    			}
    		}
    		//In one of the infuser slots; try to place in player inventory / action bar
    		else if (!this.mergeItemStack(itemstack1, 0+1, 0+37, false))
    		{
    			return null;
    		}
    		
    		if (itemstack1.stackSize == 0)
    		{
    			slot.putStack((ItemStack)null);
    		}
    		else
    		{
    			slot.onSlotChanged();
    		}
    		
    		if (itemstack1.stackSize == itemstack.stackSize)
    		{
    			return null;
    		}
    		
    		slot.onPickupFromSlot(playerIn, itemstack1);
    	}
    	return itemstack;
   	}
    
    @Override
    public void detectAndSendChanges()
    {
    	for (int i = 0; i < this.inventorySlots.size(); ++i)
        {
            ItemStack stack = ((Slot)this.inventorySlots.get(i)).getStack();
            ItemStack itemstack1 = (ItemStack)this.inventoryItemStacks.get(i);

            if (!ItemStack.areItemStacksEqual(itemstack1, stack))
            {
                itemstack1 = stack == null ? null : stack.copy();
                this.inventoryItemStacks.set(i, itemstack1);

                for (int j = 0; j < this.inventorySlots.size(); ++j)
                {
                	((ICrafting)this.crafters.get(j)).sendSlotContents(this, i, itemstack1);
                }
                if (!worldObj.isRemote && stack == cardSlot.getStackInSlot(0))
                {
                	//System.out.println("Casting");
                	if (stack != null && stack.getItem() == RoxaAccessControlMod.accessCard) 
                	{
                		ItemAccessCard card = (ItemAccessCard) stack.getItem();
            			String[] panels = card.getPanels(stack);
            			int length = 0;
            			if (panels != null) length = panels.length;
            			RoxaAccessControlMod.network.sendTo(new MessageInt(MessageInt.colorsToAdministratorGui, card.getColor(stack)), (EntityPlayerMP) player);//worldIn.getPlayerEntityByUUID(playerInventory.player.getUniqueID()));
            			RoxaAccessControlMod.network.sendTo(new MessageStringArray(MessageStringArray.panelsToAdministratorGui, panels, length), (EntityPlayerMP) player);
                	}
                	else 
                	{
                		RoxaAccessControlMod.network.sendTo(new MessageInt(MessageInt.colorsToAdministratorGui, 0), (EntityPlayerMP) player);//worldIn.getPlayerEntityByUUID(playerInventory.player.getUniqueID()));
                		RoxaAccessControlMod.network.sendTo(new MessageStringArray(MessageStringArray.panelsToAdministratorGui, null, 0), (EntityPlayerMP) player);
                	}
                }
            }
        }
    	if (cardSlot.getStackInSlot(0) == null || cardSlot.getStackInSlot(0).getItem() != RoxaAccessControlMod.accessCard) 
    	{
    		RoxaAccessControlMod.network.sendTo(new MessageInt(MessageInt.colorsToAdministratorGui, 0), (EntityPlayerMP) player);//worldIn.getPlayerEntityByUUID(playerInventory.player.getUniqueID()));
    		RoxaAccessControlMod.network.sendTo(new MessageStringArray(MessageStringArray.panelsToAdministratorGui, null, 0), (EntityPlayerMP) player);
    	}
    }
}

 

 

Edit: The solution was to update the line to:

this.listeners.get(0).sendSlotContents(this, i, itemstack1);

The whole code can be found here:

GitHub Repo

Link to comment
Share on other sites

1.9.4 changed

TileEntity

syncing a bit, the initial chunk data sent to the client now includes the update tag (

TileEntity#getUpdateTag

) of every

TileEntity

in the chunk; the update packet (

TileEntity#getUpdatePacket

) is only sent afterwards. This document by williewillus explains the changes in more detail.

 

ICrafting

was renamed to

IContainerListener

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

1.9.4 changed

TileEntity

syncing a bit, the initial chunk data sent to the client now includes the update tag (

TileEntity#getUpdateTag

) of every

TileEntity

in the chunk; the update packet (

TileEntity#getUpdatePacket

) is only sent afterwards. This document by williewillus explains the changes in more detail.

Thanks a lot! This solved it. Good explanation too :)

 

ICrafting

was renamed to

IContainerListener

.

When I chage

((ICrafting)this.crafters.get(j)).sendSlotContents(this, i, itemstack1);

to

((IContainerListener)this.inventorySlots.get(j)).sendSlotContents(this, i, itemstack1);

(as this.craftes does not seem to exist any more) it does compile but crashes as soon as I open the Gui with a "Cannot be cast" Exception. Is there anythin else but crafters and inventorySlots that I can use there? I didn't find it...

Link to comment
Share on other sites

Container#listeners

contains the

Container

's

IContainerListener

s.

 

Since generics were added to the vanilla codebase in 1.8.9 (or more accurately no longer stripped by Mojang's obfuscation process), you don't need to cast values when retrieving them from collections; the returned values are already the appropriate type. Only cast values when it's required and you already know that the value is of the type you're casting to.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

I know about Generics, I just can't see what that got to do with my problem?

 

Maybe I should be asking differently:

How do I get the client side of the Gui to immediatly display a change made to an item in one of the containers slots on the server side?

Right now the item updates client side (shows the correct tool tip) as soon as I take it out of the slot. How can I update the item so I see the correct tool tip even if I only hover it?

Link to comment
Share on other sites

On that one I can't follow you...

 

Right at the moment I'm delaying the problem by simply commenting out the line, but I haven't found a proper solution yet.

 

The basic gist of what I was saying is don't cast unless you have a reason to and you know that the value you're casting is actually an instance of the type you're casting to.

 

Container#inventorySlots

is a

List<Slot>

.

Slot

is completely unrelated to

IContainerListener

, so don't try to cast a

Slot

to

IContainerListener

.

 

Container#listeners

is a

List<IContainerListener>

. You don't need to cast values from this to

IContainerListener

because they're already that type.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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

    • im stuck in a problem whit BlockEntities i cannot solve so im trying to work around it but in the less complex way possible  i think is posible to store data to the minecraft world  in older versions people talk about  WordDataSave i dont find anything about it for 1.20.4  i find a tutorial for player capability in 1.19    here is an event      //########## ########## ########## ##########     @SubscribeEvent     public static void onAttachCapabilitiesPlayer(AttachCapabilitiesEvent<Entity> event) {         if(event.getObject() instanceof Player) {             if(!event.getObject().getCapability(PlayerThirstProvider.PLAYER_THIRST).isPresent()) {                 event.addCapability(new ResourceLocation(mercmod.MOD_ID, "properties"), new PlayerThirstProvider());             }         }     }  soo i made this      //########## ########## ########## ##########     @SubscribeEvent     public static void onAttachCapabilitiesLevel(AttachCapabilitiesEvent<Level> event) {     } i think i can do some witchcraft  here  but is many things don't understand  to start whit i dont get yet how to recall the saved data from a BlockEntity  the handler still in progress the plan is to make a Map to store nbt data using the block coordinates as index later store that map as a big nbt      ########## any way the question is if theres alredy a tutorial for 1.20.4 for this ?? or some alredy working code from someone else doing something similar ?           
    • I tried this, and found out that Oculus is the mod that is crashing the game. I have removed it, and it now works fine! Thanks!
    • Hi @DanielderErbauer, I arrived here as I have the "as it does not exist in Block{minecraft:air}" error in a modpack I am trying to start. It's a long-shot, I know, but as the post that led this to being solved is missing, can I ask if you may recall what it said? Thanks in advance
    • Hm yeah sorry, not seeing anything else that's sticking out, I'd need to debug myself probably. Do you have a github or bitbucket repo? I could poke around when I have time to see what the problem might be.
    • looking around for the capabilities thing i found a piece of code and find this piece of code  //########## ########## ########## ########## @SubscribeEvent public static void addCustomTrades(VillagerTradesEvent event) { if(event.getType() == VillagerProfession.FLETCHER) { System.out.println("\n\n### VillagerProfession.FLETCHER \n\n");//<----------- Int2ObjectMap<List<VillagerTrades.ItemListing>> trades = event.getTrades(); ItemStack stack = new ItemStack(ItemInit.ARROW_STEEL.get(), 16); int villagerLevel = 1; trades.get(villagerLevel).add((trader, rand) -> new MerchantOffer( new ItemStack(Items.EMERALD, 2), stack,1,3,0.02F)); } if(event.getType() == VillagerProfession.TOOLSMITH) { System.out.println("\n\n### VillagerProfession.TOOLSMITH \n\n");//<----------- Int2ObjectMap<List<VillagerTrades.ItemListing>> trades = event.getTrades(); ItemStack stack = new ItemStack(ItemInit.INGOT_STEEL.get(), 1); int villagerLevel = 1; trades.get(villagerLevel).add((trader, rand) -> new MerchantOffer( new ItemStack(Items.EMERALD, 2), stack,10,8,0.02F)); } } //########## ########## ########## ########## the event is aknoledge and execute when the maps load  ### VillagerProfession.FLETCHER  ### VillagerProfession.TOOLSMITH  //########## But is not working      is a random piece of code is not working  could it be than this code is old 1.18 and no valid anymore for 1.20.4 or the thing are this numbers  stack,10,8,0.02F));  i dont know what this numbers means    ########## the question is ¿how to add things to the villager traders    thanks for your attention               
  • Topics

×
×
  • Create New...

Important Information

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