Jump to content

[1.11.2] [SOLVED] Chunk Loader creation


Bektor

Recommended Posts

Hi,

 

I am currently working on implementing a chunk loader to my mod, thought I've got little to none idea how to do this.

 

How the chunk loader should work:

  • if placed by player, create chunk loader for player as long as the limit per player wasn't reached
  • otherwhise let the mod manage the chunk loader until the limit is reached (which should be equal to the limit of chunk loaders per player)
  • if the limit is reached, send a message to the placer informing him about the reached limit

 

This is the code I've got so far:

public class TileEntityChunkLoader extends TileEntityEnergy {
    
    private final String UUID_TAG = "UUID";
    
    private final int consume;
    private boolean isActive = true;
    
    private ForgeChunkManager.Ticket ticket;
    
    private UUID ownerId = null;
    
    public TileEntityChunkLoader(int capacity, int maxTransfer) {
        super(capacity, maxTransfer, false);
        
        this.consume = maxTransfer;
        this.getEnergyManager().setTransferMode(EnergyTransfer.CONSUMER);
    }
    
    @Override
    public void update() {
        super.update();
        
        if(!this.hasWorld() || this.getWorld().isRemote)
            return;
        
        this.isActive = EnergyUtils.consumeEnergy(this, this.consume);
        
        if(isActive) {
            startChunkLoading();
            
            this.getWorld().notifyBlockUpdate(this.getPos(), this.getWorld().getBlockState(this.getPos()), this.getWorld().getBlockState(this.getPos()), 3);
            this.markDirty();
        }
    }
    
    private void startChunkLoading() {
        if(this.ticket == null) {
            if(this.ownerId != null)
                this.ticket = ForgeChunkManager.requestPlayerTicket(Constants.MOD_ID, this.ownerId.toString(), this.getWorld(), Type.NORMAL);
            else
                this.ticket = ForgeChunkManager.requestTicket(Constants.MOD_ID, this.getWorld(), Type.NORMAL);
        }
        
        if(this.ticket == null) {
            JustAnotherEnergy.LOGGER.error("Ticket creation for the chunk loader at: [" + this.getPos().getX() + ", " + this.getPos().getY() + ", " + this.getPos().getZ() + "] failed!");
            return;
        }
    }

    @Override
    public SPacketUpdateTileEntity getUpdatePacket() {
        return new SPacketUpdateTileEntity(this.getPos(), 3, this.getUpdateTag());
    }
    
    @Override
    public NBTTagCompound getUpdateTag() {
        return this.writeToNBT(new NBTTagCompound());
    }
    
    @Override
    public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
        super.onDataPacket(net, pkt);
        this.handleUpdateTag(pkt.getNbtCompound());
    }
    
    @Override
    public void readFromNBT(NBTTagCompound compound) {
        this.ownerId = compound.getUniqueId(this.UUID_TAG);
        
        super.readFromNBT(compound);
    }
    
    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        compound.setUniqueId(this.UUID_TAG, this.ownerId);
        
        return super.writeToNBT(compound);
    }
public class BlockChunkLoader extends Block implements ITileEntityProvider {
    
    public static PropertyEnum<ChunkLoaderTypes> META_PROPERTY = PropertyEnum.create("loader", ChunkLoaderTypes.class);
    
    public BlockChunkLoader() {
        super(Material.IRON);
        this.setDefaultState(this.blockState.getBaseState().withProperty(META_PROPERTY, ChunkLoaderTypes.LOAD_01));
    }
    
    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
        if(worldIn.isRemote)
            return;
        
        TileEntity tile = worldIn.getTileEntity(pos);
        if(tile instanceof TileEntityChunkLoader) {
            TileEntityChunkLoader tileCL = (TileEntityChunkLoader) tile;
            
            if(placer instanceof EntityPlayer) {
                EntityPlayer player = (EntityPlayer) placer;
                tileCL.setOwnerId(player.getGameProfile().getId());
            }
        }
    }
    
    @Override
    public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
        TileEntity tile = worldIn.getTileEntity(pos);
        if(tile instanceof TileEntityChunkLoader) {
            TileEntityChunkLoader tileCL = (TileEntityChunkLoader) tile;
            
        }
        
        super.breakBlock(worldIn, pos, state);
    }
    
    @Override
    public TileEntity createNewTileEntity(World worldIn, int meta) {
        switch(meta) {
            case 0: return new TileEntityChunkLoader(500, 100); 
            case 1: return new TileEntityChunkLoader(4500, 900);
            case 2: return new TileEntityChunkLoader(125000, 25000);
            default: return new TileEntityChunkLoader(500, 100);   
        }
    }
    
    @Override
    public void getSubBlocks(Item itemIn, CreativeTabs tab, List<ItemStack> list) {
        for(final ChunkLoaderTypes type : ChunkLoaderTypes.values())
            list.add(new ItemStack(this, 1, type.getID()));
    }
    
    @Override
    public int damageDropped(IBlockState state) {
        return this.getMetaFromState(state);
    }
    
    @Override
    protected BlockStateContainer createBlockState() {
        return new BlockStateContainer(this, META_PROPERTY);
    }
    
    @Override
    public int getMetaFromState(IBlockState state) {
        return state.getValue(META_PROPERTY).getID();
    }
    
    @Override
    public IBlockState getStateFromMeta(int meta) {
        return this.getDefaultState().withProperty(META_PROPERTY, ChunkLoaderTypes.byMetadata(meta));
    }
}

 

As can be seen in the block class, the amount of energy consumption is higher when more chunks should be loaded (radius around the chunk loader including it's chunk, can also be just the chunk of the chunk loader)

 

My problem now is that I am stuck at achieving this. I am also wondering what the difference between an Entity and a Normal ChunkLoader as seen in requestTicket is.

 

Thx in advance.

Bektor

Edited by Bektor

Developer of Primeval Forest.

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.