Jump to content

TE hasn't been registered


dyno

Recommended Posts

I cannot understand what i am doing wrong.

It seems like forge never register TE: I tried to use an item to know if its TE has been registered. It says that it hasn't any TE

 

Block:

package com.olivemod.blocks.machine.energy_cell.copper;

import com.olivemod.utils.ModTileEntityTypes;
import com.olivemod.utils.Reference.NBTKeys;
import com.olivemod.utils.Reference.Reference;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.ContainerBlock;
import net.minecraft.block.RedstoneTorchBlock;
import net.minecraft.block.material.Material;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.StateContainer.Builder;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;

public class CopperCell extends Block{
	
	private static BooleanProperty energyIn = BooleanProperty.create("lit");

	public CopperCell() {
		
		super(Properties.create(Material.IRON).hardnessAndResistance(5.0f).harvestTool(ToolType.PICKAXE));
		this.setDefaultState(this.getDefaultState().with(energyIn, Boolean.valueOf(false)));
	}

	@Override
	public BlockState getStateForPlacement(BlockItemUseContext context) {

		return this.getDefaultState().with(energyIn, Boolean.valueOf(false));
	}

	@Override
	public boolean hasTileEntity() {

		return true;
	}
	
	@Override
	public TileEntity createTileEntity(BlockState state, IBlockReader world) {

		return ModTileEntityTypes.COPPER_CELL_TE.get().create();
	}
	
	@Override
	protected void fillStateContainer(Builder<Block, BlockState> builder) {

		builder.add(energyIn);
	}
	
	
	

}

TE:

package com.olivemod.blocks.machine.energy_cell.copper;

import javax.annotation.Nonnull;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;

import com.olivemod.blocks.transfer.energy.CableType;
import com.olivemod.energy.SeattableEnergyStorage.SettableEnergyStorage;
import com.olivemod.utils.ModTileEntityTypes;
import com.olivemod.utils.ModUtils;
import com.olivemod.utils.Reference.NBTKeys;
import com.sun.istack.internal.Nullable;
import net.minecraft.client.renderer.texture.ITickable;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;

public class CopperEnergyCellTE extends TileEntity implements ITickable{

	private static final int capacity = 2500;
	private static final int maxTransfer = 100;
	public final SettableEnergyStorage storage = new SettableEnergyStorage(capacity, maxTransfer);
	private final LazyOptional<IEnergyStorage> energyLazyOptional = LazyOptional.of( () -> this.storage);
	private int lastEnergy = -1;
	
	public CopperEnergyCellTE() {

		super(ModTileEntityTypes.COPPER_CELL_TE.get());
	}
	
	public CopperEnergyCellTE(TileEntityType<?> tileEntityTypeIn) {
		super(tileEntityTypeIn);
	}
	
	@Override
	public void tick() {

		transferEnergy(capacity, maxTransfer);
	}
	
	private void transferEnergy(int capacity, int maxTransfer) {
		
		for (Direction direction : ModUtils.DIRECTIONS) 
		{
			IEnergyStorage storage = world.getTileEntity(pos.offset(direction)).getCapability(CapabilityEnergy.ENERGY).orElse(null);
			if(storage != null && storage.getEnergyStored() < storage.getMaxEnergyStored() && storage.canReceive())
			{
				storage.receiveEnergy(this.storage.extractEnergy(maxTransfer, false), false);
			}
			if(storage != null && storage.getEnergyStored() > 0 && storage.canExtract())
			{
				this.storage.addEnergy(storage.extractEnergy(maxTransfer, false));
			}
			//let vanilla update chunk
			this.markDirty();	
			//Notify client of a block update
			//This will result in the packet from getUpdatePacket beong sent to the client
			//Energy will be synced
			//Flag 2 send change to client
			world.notifyBlockUpdate(pos, this.getBlockState(), this.getBlockState(), 2);
			this.lastEnergy = storage.getEnergyStored();
		}
		
	}
	
	@Override
	public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {

		this.storage.setEnergy(pkt.getNbtCompound().getInt(NBTKeys.ENERGY.getKey()));
	}

	@Override
	public CompoundNBT write(CompoundNBT compound) {

		super.write(compound);
		compound.putInt(NBTKeys.ENERGY.getKey(), this.storage.getEnergyStored());
		
		return compound;
	}
	
	@Override
	public void read(CompoundNBT compound) {

		super.read(compound);
		this.storage.setEnergy(compound.getInt(NBTKeys.ENERGY.getKey()));
	}
	
	@Override
	public <T> LazyOptional<T> getCapability(Capability<T> cap) {

		if(cap == CapabilityEnergy.ENERGY)
		{
			return this.energyLazyOptional.cast();
			
		}
		return null;
	}

	@Override
	public void onLoad() {

		if(!world.isRemote && world != null)
			this.lastEnergy = getCapability(CapabilityEnergy.ENERGY).orElse(null).getEnergyStored();
	}
	
	/*
	 * Retrieves packet to send to the client whenever this Tile Entity is re-sinced via World#notifyBlockUpdate. 
	 * This packet comes back client-side via (@link #onDataPacket)
	 */
	@Nullable
	@Override
	public SUpdateTileEntityPacket getUpdatePacket() {

		final CompoundNBT tag = new CompoundNBT();
		if(tag.contains(NBTKeys.ENERGY.getKey()))
			tag.putInt(NBTKeys.ENERGY.getKey(), this.storage.getEnergyStored());
		//We pass 0 for TileEntityTypesIn because we have a modded TE.See ClientPlayNetHandler#handlerUpdateTileEntity(SUpdateTileEntityPacket)
		return new SUpdateTileEntityPacket(this.pos, 0, tag);
	}
	
	
	/*
	 * Get an NBT compount to sync to the client with SPacketChunkData, used to initial loading of the chunk or when many blocks change at once
	 * This compound comes back to the client-side in (@link #handleUpdateTag)
	 * The default implementation ({@link TileEntity#handleUpdateTag}) calls {@link #writeInternal)}
	 * wich doesn't save any of our extra data so we override it to call {@link #write} instead 
	 */
	@Nonnull
	public CompoundNBT getUpdateTag() {

		return this.write(new CompoundNBT());
	}
}

ModTileEntityTypes:

package com.olivemod.utils;

import javax.annotation.Nonnull;

import com.olivemod.blocks.cable.energy.CopperCableTE;
import com.olivemod.blocks.machine.energy.crusher.TileEntityCrusher;
import com.olivemod.blocks.machine.energy.electric_furnace.TileEntityElectricFurnace;
import com.olivemod.blocks.machine.energy.generator.glowstone_generator.TileEntityGlowStoneGenerator;
import com.olivemod.blocks.machine.energy_cell.copper.CopperEnergyCellTE;
import com.olivemod.blocks.transfer.energy.EnergyCableTileEntity;
import com.olivemod.init.BlockInit;
import com.olivemod.utils.Reference.Reference;
import net.minecraft.tileentity.TileEntityType;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

public class ModTileEntityTypes {

	//HERE YOU REGISTER YOUR TEs
	
	public static final DeferredRegister<TileEntityType<?>> TILE_ENTITY_TYPE = new DeferredRegister<>(ForgeRegistries.TILE_ENTITIES, Reference.MOD_ID);
	
	public static final RegistryObject<TileEntityType<TileEntityGlowStoneGenerator>> GLOWSTONE_GENERATOR_TE = 
			TILE_ENTITY_TYPE.register("glowstone_generator", () -> TileEntityType.Builder.create(TileEntityGlowStoneGenerator::new, BlockInit.GLOWSTONE_GENERATOR.get()).build(null));

	public static final RegistryObject<TileEntityType<TileEntityElectricFurnace>> ELECTRIC_FURNACE_TE = 
			TILE_ENTITY_TYPE.register("electric_furnace", () -> TileEntityType.Builder.create(TileEntityElectricFurnace::new, BlockInit.ELECTRIC_FURNACE.get()).build(null));

	public static final RegistryObject<TileEntityType<TileEntityCrusher>> CRUSHER_TE = 
			TILE_ENTITY_TYPE.register("crusher", () -> TileEntityType.Builder.create(TileEntityCrusher::new,  BlockInit.CRUSHER.get()).build(null));
	
	public static final RegistryObject<TileEntityType<CopperEnergyCellTE>> COPPER_CELL_TE = 
			TILE_ENTITY_TYPE.register("copper_cell", () -> TileEntityType.Builder.create(CopperEnergyCellTE::new, BlockInit.COPPER_ENERGY_CELL.get()).build(null));
	
	public static final RegistryObject<TileEntityType<CopperCableTE>> COPPER_CABLE_TE = 
			TILE_ENTITY_TYPE.register("copper_cable", () -> TileEntityType.Builder.create(CopperCableTE::new, BlockInit.COPPER_CABLE.get()).build(null));
	
	/*public static final RegistryObject<TileEntityType<GoldCableTE>> GOLD_CABLE_TE = 
			TILE_ENTITY_TYPE.register("gold_cable", () -> TileEntityType.Builder.create(GoldCableTE::new, BlockInit.COPPER_CABLE.get()).build(null));
	
	public static final RegistryObject<TileEntityType<RedstoneCableTE>> REDSTONE_CABLE_TE = 
			TILE_ENTITY_TYPE.register("redstone_cable", () -> TileEntityType.Builder.create(RedstoneCableTE::new, BlockInit.COPPER_CABLE.get()).build(null));
*/
	
}

 

2020-05-31_21.19.17.png

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.