NaturesAura/src/main/java/de/ellpeck/naturesaura/api/aura/container/ItemAuraContainer.java

70 lines
1.7 KiB
Java
Raw Normal View History

2018-11-11 13:26:19 +01:00
package de.ellpeck.naturesaura.api.aura.container;
2018-10-20 21:19:08 +02:00
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-08 00:31:29 +01:00
import net.minecraft.world.item.ItemStack;
2018-10-20 21:19:08 +02:00
public class ItemAuraContainer implements IAuraContainer {
protected final ItemStack stack;
protected final IAuraType type;
2018-10-20 21:19:08 +02:00
protected final int maxAura;
public ItemAuraContainer(ItemStack stack, IAuraType type, int maxAura) {
2018-10-20 21:19:08 +02:00
this.stack = stack;
this.type = type;
2018-10-20 21:19:08 +02:00
this.maxAura = maxAura;
}
@Override
public int storeAura(int amountToStore, boolean simulate) {
2021-12-15 16:30:22 +01:00
var aura = this.getStoredAura();
var actual = Math.min(amountToStore, this.getMaxAura() - aura);
2018-10-20 21:19:08 +02:00
if (!simulate) {
this.setAura(aura + actual);
}
return actual;
}
@Override
public int drainAura(int amountToDrain, boolean simulate) {
2021-12-15 16:30:22 +01:00
var aura = this.getStoredAura();
var actual = Math.min(amountToDrain, aura);
2018-10-20 21:19:08 +02:00
if (!simulate) {
this.setAura(aura - actual);
}
return actual;
}
private void setAura(int amount) {
2020-01-21 23:02:39 +01:00
if (!this.stack.hasTag()) {
2021-12-04 15:40:09 +01:00
this.stack.setTag(new CompoundTag());
2018-10-20 21:19:08 +02:00
}
2020-01-21 23:02:39 +01:00
this.stack.getTag().putInt("aura", amount);
2018-10-20 21:19:08 +02:00
}
@Override
public int getStoredAura() {
2020-01-21 23:02:39 +01:00
if (this.stack.hasTag()) {
return this.stack.getTag().getInt("aura");
2018-10-20 21:19:08 +02:00
} else {
return 0;
}
}
@Override
public int getMaxAura() {
return this.maxAura;
}
@Override
public int getAuraColor() {
return 0x42a6bc;
}
@Override
public boolean isAcceptableType(IAuraType type) {
return this.type == null || this.type == type;
}
2018-10-20 21:19:08 +02:00
}