-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathElectricStats.java
More file actions
152 lines (131 loc) · 6.67 KB
/
ElectricStats.java
File metadata and controls
152 lines (131 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package gregtech.api.items.metaitem;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.IElectricItem;
import gregtech.api.capability.impl.ElectricItem;
import gregtech.api.items.metaitem.stats.IItemBehaviour;
import gregtech.api.items.metaitem.stats.IItemCapabilityProvider;
import gregtech.api.items.metaitem.stats.IItemMaxStackSizeProvider;
import gregtech.api.items.metaitem.stats.IItemComponent;
import gregtech.api.util.LocalisationUtils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import java.util.List;
public class ElectricStats implements IItemComponent, IItemCapabilityProvider, IItemMaxStackSizeProvider, IItemBehaviour {
public static final ElectricStats EMPTY = new ElectricStats(0, 0, false, false);
public final long maxCharge;
public final int tier;
public final boolean chargeable;
public final boolean dischargeable;
public ElectricStats(long maxCharge, long tier, boolean chargeable, boolean dischargeable) {
this.maxCharge = maxCharge;
this.tier = (int) tier;
this.chargeable = chargeable;
this.dischargeable = dischargeable;
}
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
ItemStack itemStack = player.getHeldItem(hand);
IElectricItem electricItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
if(electricItem != null && electricItem.canProvideChargeExternally() && player.isSneaking()) {
if(!world.isRemote) {
boolean isInDischargeMode = isInDishargeMode(itemStack);
String locale = "metaitem.electric.discharge_mode." + (isInDischargeMode ? "disabled" : "enabled");
player.sendStatusMessage(new TextComponentTranslation(locale), true);
setInDischargeMode(itemStack, !isInDischargeMode);
}
return ActionResult.newResult(EnumActionResult.SUCCESS, itemStack);
}
return ActionResult.newResult(EnumActionResult.PASS, itemStack);
}
@Override
public void onUpdate(ItemStack itemStack, Entity entity) {
IElectricItem electricItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
if(!entity.world.isRemote && entity instanceof EntityPlayer && electricItem != null &&
electricItem.canProvideChargeExternally() &&
isInDishargeMode(itemStack) && electricItem.getCharge() > 0L) {
EntityPlayer entityPlayer = (EntityPlayer) entity;
InventoryPlayer inventoryPlayer = entityPlayer.inventory;
long transferLimit = electricItem.getTransferLimit();
for(int i = 0; i < inventoryPlayer.getSizeInventory(); i++) {
ItemStack itemInSlot = inventoryPlayer.getStackInSlot(i);
IElectricItem slotElectricItem = itemInSlot.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
if(slotElectricItem != null && !slotElectricItem.canProvideChargeExternally()) {
long chargedAmount = chargeElectricItem(transferLimit, electricItem, slotElectricItem);
if(chargedAmount > 0L) {
transferLimit -= chargedAmount;
if(transferLimit == 0L) break;
}
}
}
}
}
private static long chargeElectricItem(long maxDischargeAmount, IElectricItem source, IElectricItem target) {
long maxDischarged = source.discharge(maxDischargeAmount, source.getTier(), false, false, true);
long maxReceived = target.charge(maxDischarged, source.getTier(), false, true);
if(maxReceived > 0L) {
long resultDischarged = source.discharge(maxReceived, source.getTier(), false, true, false);
target.charge(resultDischarged, source.getTier(), false, false);
return resultDischarged;
}
return 0L;
}
private static void setInDischargeMode(ItemStack itemStack, boolean isDischargeMode) {
if(isDischargeMode) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if(tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
tagCompound.setBoolean("DischargeMode", true);
} else {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if(tagCompound != null) {
tagCompound.removeTag("DischargeMode");
if(tagCompound.isEmpty()) {
itemStack.setTagCompound(null);
}
}
}
}
@Override
public void addInformation(ItemStack itemStack, List<String> lines) {
IElectricItem electricItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
if(electricItem != null && electricItem.canProvideChargeExternally()) {
lines.add(LocalisationUtils.format("metaitem.electric.discharge_mode.tooltip"));
}
}
private static boolean isInDishargeMode(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
return tagCompound != null && tagCompound.getBoolean("DischargeMode");
}
@Override
public int getMaxStackSize(ItemStack itemStack, int defaultValue) {
ElectricItem electricItem = (ElectricItem) itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
if (electricItem == null || electricItem.getCharge() == 0) {
return defaultValue;
}
return 1;
}
@Override
public ICapabilityProvider createProvider(ItemStack itemStack) {
return new ElectricItem(itemStack, maxCharge, tier, chargeable, dischargeable);
}
public static ElectricStats createElectricItem(long maxCharge, long tier) {
return new ElectricStats(maxCharge, tier, true, false);
}
public static ElectricStats createRechargeableBattery(long maxCharge, int tier) {
return new ElectricStats(maxCharge, tier, true, true);
}
public static ElectricStats createBattery(long maxCharge, int tier, boolean rechargeable) {
return new ElectricStats(maxCharge, tier, rechargeable, true);
}
}