Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hello everyone!

 

When i put battery in generator's charge slot, others battery in inventory get It. It happens in the creative tab, too.

Seems like they're a single item. I suppose to fail something in NBT

 

package olivemod.items.BaseItemElectric.battery;

import java.util.List;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTPrimitive;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagFloat;
import net.minecraft.network.play.client.CPacketAnimation;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.Capability.IStorage;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.common.registry.GameRegistry;
import olivemod.Main;
import olivemod.energy.CustomEnergyStorage;
import olivemod.energy.item_energy.ItemEnergy;
import olivemod.init.ItemInit;
import olivemod.items.ItemBase;
import olivemod.util.IHasModels;

public class Battery extends ItemBase implements IEnergyStorage, IStorage<IEnergyStorage>
{

	public static final Capability<IEnergyStorage> CAPABILITY_STORAGE = null;
	public CustomEnergyStorage storage = new CustomEnergyStorage(1000, 100, 100);
	private final int capacity;
	private final int maxReceive;
	private final int maxExtract; 
	
	public Battery(String name, int capacity, int maxIn, int maxOut) {

		super(name);
		setHasSubtypes(true);
		setMaxStackSize(1);
		setNoRepair();
		
		this.capacity = capacity;
		this.maxExtract = maxOut;
		this.maxReceive = maxIn;
		
	}
	
	@Override
	public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt) {
		// TODO Auto-generated method stub
		return new ICapabilityProvider() {
			
			@Override
			public boolean hasCapability(Capability<?> capability, EnumFacing facing) {

				if(capability == CapabilityEnergy.ENERGY) 
					return true;				
				return false;
			}
			
			@Override
			public <T> T getCapability(Capability<T> capability, EnumFacing facing) {

				if(capability == CapabilityEnergy.ENERGY)
					return (T) CapabilityEnergy.ENERGY.cast(storage);
				
				return null;
			}
		};
	}

	@Override
	public int receiveEnergy(int maxReceive, boolean simulate) {

		return 0;
	}

	@Override
	public int extractEnergy(int maxExtract, boolean simulate) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getEnergyStored() {
		// TODO Auto-generated method stub
		return this.storage.getEnergyStored();
	}

	@Override
	public int getMaxEnergyStored() {

		return this.storage.getMaxEnergyStored();
	}

	@Override
	public boolean canExtract() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean canReceive() {
		// TODO Auto-generated method stub
		return true;
	}
	
	@Override
	public boolean showDurabilityBar(ItemStack stack) {
		// TODO Auto-generated method stub
		return true;
	}
	
	@Override
	public double getDurabilityForDisplay(ItemStack stack) {
		
		if(isInCreativeTab(getCreativeTab()))
			return 1.0;
		
		if(stack.hasCapability(CapabilityEnergy.ENERGY, null))
		{
			IEnergyStorage storage1 = stack.getCapability(CapabilityEnergy.ENERGY, null);
				return (double)(this.capacity / this.storage.getEnergyStored());
			
		}
		return 0;
	}
	
	@Override
	public void addInformation(ItemStack stack, World worldIn, List<String> tooltip, ITooltipFlag flagIn) {

		tooltip.add(this.storage.getEnergyStored() + "/" + this.capacity);
	}


	@Override
	public NBTBase writeNBT(Capability<IEnergyStorage> capability, IEnergyStorage instance, EnumFacing side) {

		return new NBTTagFloat(instance.getEnergyStored());
	}

	@Override
	public void readNBT(Capability<IEnergyStorage> capability, IEnergyStorage instance, EnumFacing side, NBTBase nbt) {
		
		instance.receiveEnergy((int) ((NBTPrimitive)nbt).getFloat(), false);
	}
	
}

 

Edited by dyno

2 minutes ago, dyno said:

Hello everyone!

 

When i put battery in generator's charge slot, others battery in inventory get It. It happens in the creative tab, too.

Seems like they're a single item. I suppose to fail something in NBT

You've given literally no context at all, post code, preferably as a Git repository.

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

  • Author
5 minutes ago, Novârch said:

You've given literally no context at all, post code, preferably as a Git repository.

Yes, I know. I had it in another device. I gave it right now.

Guest
This topic is now closed to further replies.

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.