Implemented drain/fill in particle API

This commit is contained in:
LemADEC 2017-02-25 23:09:21 +01:00
parent 4cbc277de1
commit e2d963c071
5 changed files with 85 additions and 31 deletions

View file

@ -4,13 +4,17 @@ import net.minecraft.item.ItemStack;
public interface IParticleContainerItem {
ParticleStack getParticle(ItemStack container);
ParticleStack getParticleStack(ItemStack container);
int getCapacity(ItemStack container);
boolean isEmpty(ItemStack container);
// fills the container and return how much could be transferred or 0 if container is empty or contains different particles
int fill(ItemStack container, ParticleStack resource, boolean doFill);
ParticleStack drain(ItemStack container, int maxDrain, boolean doDrain);
// drains the container and return how much could be transferred or null if container is empty or contains different particles
ParticleStack drain(ItemStack container, ParticleStack resource, boolean doDrain);
// called during recipe match to set amount to consume in next call to getContainerItem
void setAmountToConsume(ItemStack container, int amount);

View file

@ -67,6 +67,8 @@ public class ParticleStack {
return particle;
}
public boolean isEmpty() { return particle == null || amount <= 0; }
public final int getAmount() {
return amount;
}
@ -83,8 +85,11 @@ public class ParticleStack {
return this.getParticle().getUnlocalizedName();
}
public ParticleStack copy()
{
public ParticleStack copy() {
return new ParticleStack(getParticle(), amount, tag);
}
public ParticleStack copy(final int amount) {
return new ParticleStack(getParticle(), amount, tag);
}
@ -114,7 +119,7 @@ public class ParticleStack {
}
if (other.getItem() instanceof IParticleContainerItem) {
return isParticleEqual(((IParticleContainerItem) other.getItem()).getParticle(other));
return isParticleEqual(((IParticleContainerItem) other.getItem()).getParticleStack(other));
}
return false;

View file

@ -202,10 +202,10 @@ public class RecipeParticleShapedOre implements IRecipe {
&& itemStackSlot.getItem() instanceof IParticleContainerItem
&& ((ItemStack) target).getItem() instanceof IParticleContainerItem) {
IParticleContainerItem particleContainerItemSlot = (IParticleContainerItem) itemStackSlot.getItem();
ParticleStack particleStackSlot = particleContainerItemSlot.getParticle(itemStackSlot);
ParticleStack particleStackSlot = particleContainerItemSlot.getParticleStack(itemStackSlot);
IParticleContainerItem particleContainerItemTarget = (IParticleContainerItem) ((ItemStack) target).getItem();
ParticleStack particleStackTarget = particleContainerItemTarget.getParticle((ItemStack) target);
ParticleStack particleStackTarget = particleContainerItemTarget.getParticleStack((ItemStack) target);
// reject different particles or insufficient quantity
if (!particleStackSlot.containsParticle(particleStackTarget)) {

View file

@ -3,6 +3,7 @@ package cr0s.warpdrive.entity;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.config.WarpDriveConfig;
import cr0s.warpdrive.data.Vector3;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
@ -28,7 +29,7 @@ public class EntityParticleBunch extends Entity {
}
}
public EntityParticleBunch(World world, int x, int y, int z) {
public EntityParticleBunch(World world, final double x, final double y, final double z) {
super(world);
this.posX = x + 0.5D;
this.posY = y + 0.5D;
@ -114,6 +115,23 @@ public class EntityParticleBunch extends Entity {
}
}
@Override
public void writeToNBT(NBTTagCompound p_70109_1_) {
super.writeToNBT(p_70109_1_);
}
// prevent saving entity to chunk
@Override
public boolean writeMountToNBT(NBTTagCompound p_98035_1_) {
return false;
}
// prevent saving entity to chunk
@Override
public boolean writeToNBTOptional(NBTTagCompound p_70039_1_) {
return false;
}
@Override
public String toString() {
return String.format("%s/%d @ \'%s\' %.2f %.2f %.2f",

View file

@ -119,7 +119,7 @@ public class ItemElectromagneticCell extends Item implements IParticleContainerI
@Override
public ItemStack getContainerItem(ItemStack itemStackFilled) {
ParticleStack particleStack = getParticle(itemStackFilled);
ParticleStack particleStack = getParticleStack(itemStackFilled);
if (particleStack != null) {
final int amount = particleStack.getAmount() - getAmountToConsume(itemStackFilled);
if (amount <= 0) {
@ -132,7 +132,7 @@ public class ItemElectromagneticCell extends Item implements IParticleContainerI
@Override
public void setAmountToConsume(ItemStack itemStack, int amountToConsume) {
ParticleStack particleStack = getParticle(itemStack);
ParticleStack particleStack = getParticleStack(itemStack);
if (particleStack == null || particleStack.getParticle() == null) {
return;
}
@ -167,7 +167,7 @@ public class ItemElectromagneticCell extends Item implements IParticleContainerI
}
@Override
public ParticleStack getParticle(ItemStack itemStack) {
public ParticleStack getParticleStack(final ItemStack itemStack) {
if (itemStack.getItem() != this || !itemStack.hasTagCompound()) {
return null;
}
@ -179,32 +179,59 @@ public class ItemElectromagneticCell extends Item implements IParticleContainerI
}
@Override
public int getCapacity(ItemStack container) {
public int getCapacity(final ItemStack container) {
return 1000;
}
@Override
public int fill(ItemStack itemStack, ParticleStack resource, boolean doFill) {
ParticleStack particleStack = getParticle(itemStack);
if (particleStack == null || particleStack.getParticle() == null) {
particleStack = new ParticleStack(resource.getParticle(), 0);
} else if (!particleStack.containsParticle(resource) || particleStack.getAmount() >= getCapacity(itemStack)) {
return 0;
}
int consumable = Math.min(resource.getAmount(), getCapacity(itemStack) - particleStack.getAmount());
if (!doFill) {
particleStack.fill(consumable);
NBTTagCompound tagCompound = itemStack.hasTagCompound() ? itemStack.getTagCompound() : new NBTTagCompound();
tagCompound.setTag("particle", particleStack.writeToNBT(new NBTTagCompound()));
updateDamageLevel(itemStack, particleStack);
}
return consumable;
public boolean isEmpty(final ItemStack itemStack) {
ParticleStack particleStack = getParticleStack(itemStack);
return particleStack == null || particleStack.isEmpty();
}
@Override
public ParticleStack drain(ItemStack container, int maxDrain, boolean doDrain) {
return null; // @TODO not implemented
public int fill(ItemStack itemStack, final ParticleStack resource, final boolean doFill) {
ParticleStack particleStack = getParticleStack(itemStack);
if (particleStack == null || particleStack.getParticle() == null) {
particleStack = new ParticleStack(resource.getParticle(), 0);
} else if (!particleStack.isParticleEqual(resource) || particleStack.getAmount() >= getCapacity(itemStack)) {
return 0;
}
int transfer = Math.min(resource.getAmount(), getCapacity(itemStack) - particleStack.getAmount());
if (doFill) {
particleStack.fill(transfer);
NBTTagCompound tagCompound = itemStack.hasTagCompound() ? itemStack.getTagCompound() : new NBTTagCompound();
tagCompound.setTag("particle", particleStack.writeToNBT(new NBTTagCompound()));
if (!itemStack.hasTagCompound()) {
itemStack.setTagCompound(tagCompound);
}
updateDamageLevel(itemStack, particleStack);
}
return transfer;
}
@Override
public ParticleStack drain(ItemStack itemStack, final ParticleStack resource, final boolean doDrain) {
ParticleStack particleStack = getParticleStack(itemStack);
if (particleStack == null || particleStack.getParticle() == null) {
return null;
}
if (!particleStack.isParticleEqual(resource) || particleStack.getAmount() <= 0) {
return null;
}
int transfer = Math.min(resource.getAmount(), particleStack.getAmount());
if (doDrain) {
particleStack.fill(-transfer);
NBTTagCompound tagCompound = itemStack.hasTagCompound() ? itemStack.getTagCompound() : new NBTTagCompound();
tagCompound.setTag("particle", particleStack.writeToNBT(new NBTTagCompound()));
if (!itemStack.hasTagCompound()) {
itemStack.setTagCompound(tagCompound);
}
updateDamageLevel(itemStack, particleStack);
}
return resource.copy(transfer);
}
@Override
@ -216,7 +243,7 @@ public class ItemElectromagneticCell extends Item implements IParticleContainerI
return;
}
final ItemElectromagneticCell itemElectromagneticCell = (ItemElectromagneticCell) itemStack.getItem();
final ParticleStack particleStack = itemElectromagneticCell.getParticle(itemStack);
final ParticleStack particleStack = itemElectromagneticCell.getParticleStack(itemStack);
String tooltip;
if (particleStack == null || particleStack.getParticle() == null) {
tooltip = StatCollector.translateToLocalFormatted("item.warpdrive.atomic.electromagnetic_cell.tooltip.empty");