Skip to content

Commit 6c1bceb

Browse files
authored
Rename methods to clearly indicate that they have side effects (#41)
1 parent c21adfa commit 6c1bceb

13 files changed

Lines changed: 33 additions & 31 deletions

File tree

src/main/java/com/darkona/adventurebackpack/block/TileAdventureBackpack.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static com.darkona.adventurebackpack.common.Constants.TAG_DISABLE_NVISION;
99
import static com.darkona.adventurebackpack.common.Constants.TAG_EXTENDED_COMPOUND;
1010
import static com.darkona.adventurebackpack.common.Constants.TAG_INVENTORY;
11+
import static com.darkona.adventurebackpack.common.Constants.TAG_LAST_TIME;
1112
import static com.darkona.adventurebackpack.common.Constants.TAG_LEFT_TANK;
1213
import static com.darkona.adventurebackpack.common.Constants.TAG_RIGHT_TANK;
1314
import static com.darkona.adventurebackpack.common.Constants.TAG_TYPE;
@@ -134,7 +135,7 @@ public void loadFromNBT(NBTTagCompound compound) {
134135
extendedProperties = backpackTag.getCompoundTag(TAG_EXTENDED_COMPOUND);
135136
disableCycling = backpackTag.getBoolean(TAG_DISABLE_CYCLING);
136137
disableNVision = backpackTag.getBoolean(TAG_DISABLE_NVISION);
137-
lastTime = backpackTag.getInteger("lastTime");
138+
lastTime = backpackTag.getInteger(TAG_LAST_TIME);
138139
}
139140

140141
@Override
@@ -161,7 +162,7 @@ public void saveToNBT(NBTTagCompound compound) {
161162
backpackTag.setTag(TAG_EXTENDED_COMPOUND, extendedProperties);
162163
backpackTag.setBoolean(TAG_DISABLE_CYCLING, disableCycling);
163164
backpackTag.setBoolean(TAG_DISABLE_NVISION, disableNVision);
164-
backpackTag.setInteger("lastTime", lastTime);
165+
backpackTag.setInteger(TAG_LAST_TIME, lastTime);
165166

166167
compound.setTag(TAG_WEARABLE_COMPOUND, backpackTag);
167168
}

src/main/java/com/darkona/adventurebackpack/client/audio/CopterPackSound.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void update() {
5555
return;
5656
}
5757

58-
status = BackpackUtils.getWearableCompound(copter).getByte(TAG_STATUS);
58+
status = BackpackUtils.getOrCreateWearableCompound(copter).getByte(TAG_STATUS);
5959
if (status == ItemCopterPack.OFF_MODE) {
6060
setDonePlaying();
6161
} else if (status == ItemCopterPack.HOVER_MODE) {

src/main/java/com/darkona/adventurebackpack/client/models/ModelCopterPack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private void renderCopterPack(Entity entity, float scale) {
183183
InventoryCopterPack copterInv = new InventoryCopterPack(this.copterPack);
184184
copterInv.openInventory();
185185
Axis.isHidden = true;
186-
if (BackpackUtils.getWearableCompound(copterPack).getByte(TAG_STATUS) != ItemCopterPack.OFF_MODE) {
186+
if (BackpackUtils.getOrCreateWearableCompound(copterPack).getByte(TAG_STATUS) != ItemCopterPack.OFF_MODE) {
187187
Axis.isHidden = false;
188188
int degrees;
189189
if (entity.onGround || (entity.isSneaking())) {

src/main/java/com/darkona/adventurebackpack/common/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public enum Source // TODO move to separate class?
4545
public static final String TAG_RIGHT_TANK = "rightTank";
4646
public static final String TAG_DISABLE_CYCLING = "disableCycling";
4747
public static final String TAG_DISABLE_NVISION = "disableNVision";
48+
public static final String TAG_LAST_TIME = "lastTime";
4849

4950
// NBT: Extended Properties
5051
public static final String TAG_HOLDING_SPACE = "holdingSpace";

src/main/java/com/darkona/adventurebackpack/common/ServerActions.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,16 @@ public static void pistonBootsJump(EntityPlayer player) {
250250
}
251251

252252
public static void copterSoundAtLogin(EntityPlayer player) {
253-
byte status = BackpackUtils.getWearableCompound(BackpackProperty.get(player).getWearable()).getByte(TAG_STATUS);
253+
byte status = BackpackUtils.getOrCreateWearableCompound(BackpackProperty.get(player).getWearable())
254+
.getByte(TAG_STATUS);
254255

255256
if (!player.worldObj.isRemote && status != ItemCopterPack.OFF_MODE) {
256257
ModNetwork.sendToNearby(new EntitySoundPacket.Message(EntitySoundPacket.COPTER_SOUND, player), player);
257258
}
258259
}
259260

260261
public static void jetpackSoundAtLogin(EntityPlayer player) {
261-
boolean isBoiling = BackpackUtils.getWearableCompound(BackpackProperty.get(player).getWearable())
262+
boolean isBoiling = BackpackUtils.getOrCreateWearableCompound(BackpackProperty.get(player).getWearable())
262263
.getBoolean("boiling");
263264

264265
if (!player.worldObj.isRemote && isBoiling) {
@@ -271,7 +272,7 @@ public static void jetpackSoundAtLogin(EntityPlayer player) {
271272
public static void toggleCopterPack(EntityPlayer player, ItemStack copter, byte type) {
272273
String message = "";
273274
boolean actionPerformed = false;
274-
byte mode = BackpackUtils.getWearableCompound(copter).getByte(TAG_STATUS);
275+
byte mode = BackpackUtils.getOrCreateWearableCompound(copter).getByte(TAG_STATUS);
275276
byte newMode = ItemCopterPack.OFF_MODE;
276277

277278
if (type == WearableModePacket.COPTER_ON_OFF) {
@@ -305,7 +306,7 @@ public static void toggleCopterPack(EntityPlayer player, ItemStack copter, byte
305306
}
306307

307308
if (actionPerformed) {
308-
BackpackUtils.getWearableCompound(copter).setByte(TAG_STATUS, newMode);
309+
BackpackUtils.getOrCreateWearableCompound(copter).setByte(TAG_STATUS, newMode);
309310
if (player.worldObj.isRemote && ConfigHandler.chatSpam) {
310311
player.addChatComponentMessage(new ChatComponentTranslation(message));
311312
}

src/main/java/com/darkona/adventurebackpack/init/recipes/ShapedBackpackRecipe.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public ItemStack getCraftingResult(InventoryCrafting craftMatrix) {
3838
for (int i = 0; i < craftMatrix.getSizeInventory(); i++) {
3939
ItemStack matrixStack = craftMatrix.getStackInSlot(i);
4040
if (matrixStack != null && matrixStack.getItem() == ModItems.adventureBackpack
41-
&& BackpackUtils.getWearableCompound(matrixStack).hasKey(TAG_INVENTORY)) {
42-
NBTTagList itemList = BackpackUtils.getWearableInventory(matrixStack);
43-
BackpackUtils.getWearableCompound(craftResult).setTag(TAG_INVENTORY, itemList);
41+
&& BackpackUtils.getOrCreateWearableCompound(matrixStack).hasKey(TAG_INVENTORY)) {
42+
NBTTagList itemList = BackpackUtils.getOrCreateWearableInventory(matrixStack);
43+
BackpackUtils.getOrCreateWearableCompound(craftResult).setTag(TAG_INVENTORY, itemList);
4444

4545
LogHelper.info(
4646
"Successfully transferred inventory from the ingredient backpack ["

src/main/java/com/darkona/adventurebackpack/inventory/InventoryAdventure.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void dirtyInventory() {
138138
}
139139

140140
protected NBTTagCompound getWearableCompound() {
141-
return BackpackUtils.getWearableCompound(containerStack);
141+
return BackpackUtils.getOrCreateWearableCompound(containerStack);
142142
}
143143

144144
protected void setInventoryFromTagList(NBTTagList items) {

src/main/java/com/darkona/adventurebackpack/inventory/InventoryBackpack.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static com.darkona.adventurebackpack.common.Constants.TAG_DISABLE_NVISION;
99
import static com.darkona.adventurebackpack.common.Constants.TAG_EXTENDED_COMPOUND;
1010
import static com.darkona.adventurebackpack.common.Constants.TAG_INVENTORY;
11+
import static com.darkona.adventurebackpack.common.Constants.TAG_LAST_TIME;
1112
import static com.darkona.adventurebackpack.common.Constants.TAG_LEFT_TANK;
1213
import static com.darkona.adventurebackpack.common.Constants.TAG_RIGHT_TANK;
1314
import static com.darkona.adventurebackpack.common.Constants.TAG_TYPE;
@@ -103,7 +104,7 @@ public void loadFromNBT(NBTTagCompound compound) {
103104
loadSleepingBag();
104105
disableCycling = backpackTag.getBoolean(TAG_DISABLE_CYCLING);
105106
disableNVision = backpackTag.getBoolean(TAG_DISABLE_NVISION);
106-
lastTime = backpackTag.getInteger("lastTime");
107+
lastTime = backpackTag.getInteger(TAG_LAST_TIME);
107108
}
108109

109110
@Override
@@ -117,7 +118,7 @@ public void saveToNBT(NBTTagCompound compound) {
117118
saveSleepingBag();
118119
backpackTag.setBoolean(TAG_DISABLE_CYCLING, disableCycling);
119120
backpackTag.setBoolean(TAG_DISABLE_NVISION, disableNVision);
120-
backpackTag.setInteger("lastTime", lastTime);
121+
backpackTag.setInteger(TAG_LAST_TIME, lastTime);
121122

122123
compound.setTag(TAG_WEARABLE_COMPOUND, backpackTag);
123124
}
@@ -145,7 +146,7 @@ public void dirtyExtended() // TODO is it redundant?
145146

146147
@Override
147148
public void dirtyTime() {
148-
getWearableCompound().setInteger("lastTime", lastTime);
149+
getWearableCompound().setInteger(TAG_LAST_TIME, lastTime);
149150
}
150151

151152
@Override

src/main/java/com/darkona/adventurebackpack/item/ItemAdventureBackpack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void getSubItems(Item item, CreativeTabs par2CreativeTabs, List subItems)
7272
@SuppressWarnings({ "unchecked" })
7373
@SideOnly(Side.CLIENT)
7474
public void addInformation(ItemStack stack, EntityPlayer player, List tooltips, boolean advanced) {
75-
NBTTagCompound backpackTag = BackpackUtils.getWearableCompound(stack);
75+
NBTTagCompound backpackTag = BackpackUtils.getOrCreateWearableCompound(stack);
7676

7777
BackpackTypes type = BackpackTypes.getType(backpackTag.getByte(TAG_TYPE));
7878
tooltips.add(Utils.getColoredSkinName(type));
@@ -278,7 +278,7 @@ public double getDurabilityForDisplay(ItemStack stack) {
278278
}
279279

280280
private int getItemCount(ItemStack backpack) {
281-
NBTTagList itemList = BackpackUtils.getWearableInventory(backpack);
281+
NBTTagList itemList = BackpackUtils.getOrCreateWearableInventory(backpack);
282282
int itemCount = itemList.tagCount();
283283
for (int i = itemCount - 1; i >= 0; i--) {
284284
int slotAtI = itemList.getCompoundTagAt(i).getInteger(Constants.TAG_SLOT);

src/main/java/com/darkona/adventurebackpack/item/ItemCoalJetpack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void getSubItems(Item item, CreativeTabs tab, List list) {
5858
public void addInformation(ItemStack stack, EntityPlayer player, List tooltips, boolean advanced) {
5959
FluidTank waterTank = new FluidTank(Constants.Jetpack.WATER_CAPACITY);
6060
FluidTank steamTank = new FluidTank(Constants.Jetpack.STEAM_CAPACITY);
61-
NBTTagCompound jetpackTag = BackpackUtils.getWearableCompound(stack);
61+
NBTTagCompound jetpackTag = BackpackUtils.getOrCreateWearableCompound(stack);
6262

6363
if (GuiScreen.isShiftKeyDown()) {
6464
NBTTagList itemList = jetpackTag.getTagList(Constants.TAG_INVENTORY, NBT.TAG_COMPOUND);
@@ -265,7 +265,7 @@ public double getDurabilityForDisplay(ItemStack stack) {
265265
}
266266

267267
private int getTemperature(ItemStack jetpack) {
268-
return BackpackUtils.getWearableCompound(jetpack).getInteger("temperature");
268+
return BackpackUtils.getOrCreateWearableCompound(jetpack).getInteger("temperature");
269269
}
270270

271271
@Override

0 commit comments

Comments
 (0)