Skip to content

Commit d31b428

Browse files
authored
Merge pull request #161 from Alecsioo/machinery_hatches_copy_paste_settings
Implemented the ability to copy-paste machinery hatch settings
2 parents a8aa7b2 + 5c1229e commit d31b428

File tree

9 files changed

+141
-14
lines changed

9 files changed

+141
-14
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ dependencies {
202202
implementation(rfg.deobf("curse.maven:thermal-expansion-69163:2926431"))
203203

204204
// AE2 Compat
205-
// implementation(rfg.deobf("curse.maven:applied-energistics-2-223794:2747063"))
205+
// implementation(rfg.deobf("curse.maven:applied-energistics-2-223794:2747063"))
206206
implementation(rfg.deobf("curse.maven:ae2-extended-life-570458:6302098"))
207207
implementation(rfg.deobf("curse.maven:ae2-fluid-crafting-rework-623955:5504001"))
208208
implementation(rfg.deobf("curse.maven:mekanism-energistics-1027681:5408319"))

src/main/java/github/kasuminova/mmce/common/block/appeng/BlockMEFluidInputBus.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package github.kasuminova.mmce.common.block.appeng;
22

3+
import appeng.api.implementations.items.IMemoryCard;
34
import github.kasuminova.mmce.common.tile.MEFluidInputBus;
45
import hellfirepvp.modularmachinery.ModularMachinery;
56
import hellfirepvp.modularmachinery.common.CommonProxy;
67
import net.minecraft.block.state.IBlockState;
78
import net.minecraft.entity.player.EntityPlayer;
9+
import net.minecraft.item.ItemStack;
810
import net.minecraft.tileentity.TileEntity;
911
import net.minecraft.util.EnumFacing;
1012
import net.minecraft.util.EnumHand;
@@ -20,7 +22,15 @@ public class BlockMEFluidInputBus extends BlockMEFluidBus {
2022
public boolean onBlockActivated(@Nonnull final World worldIn, @Nonnull final BlockPos pos, @Nonnull final IBlockState state, @Nonnull final EntityPlayer playerIn, @Nonnull final EnumHand hand, @Nonnull final EnumFacing facing, final float hitX, final float hitY, final float hitZ) {
2123
if (!worldIn.isRemote) {
2224
TileEntity te = worldIn.getTileEntity(pos);
23-
if (te instanceof MEFluidInputBus) {
25+
if (te instanceof MEFluidInputBus inputBus) {
26+
ItemStack heldItem = playerIn.getHeldItem(hand);
27+
if (heldItem.getItem() instanceof IMemoryCard memoryCard) {
28+
boolean handled = handleSettingsTransfer(inputBus, memoryCard, playerIn, heldItem);
29+
if (handled) {
30+
return true;
31+
}
32+
}
33+
2434
playerIn.openGui(ModularMachinery.MODID, CommonProxy.GuiType.ME_FLUID_INPUT_BUS.ordinal(), worldIn, pos.getX(), pos.getY(), pos.getZ());
2535
}
2636
}

src/main/java/github/kasuminova/mmce/common/block/appeng/BlockMEGasInputBus.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package github.kasuminova.mmce.common.block.appeng;
22

3+
import appeng.api.implementations.items.IMemoryCard;
34
import github.kasuminova.mmce.common.tile.MEGasInputBus;
45
import hellfirepvp.modularmachinery.ModularMachinery;
56
import hellfirepvp.modularmachinery.common.CommonProxy;
67
import net.minecraft.block.state.IBlockState;
78
import net.minecraft.entity.player.EntityPlayer;
9+
import net.minecraft.item.ItemStack;
810
import net.minecraft.tileentity.TileEntity;
911
import net.minecraft.util.EnumFacing;
1012
import net.minecraft.util.EnumHand;
@@ -20,7 +22,15 @@ public class BlockMEGasInputBus extends BlockMEGasBus {
2022
public boolean onBlockActivated(@Nonnull final World worldIn, @Nonnull final BlockPos pos, @Nonnull final IBlockState state, @Nonnull final EntityPlayer playerIn, @Nonnull final EnumHand hand, @Nonnull final EnumFacing facing, final float hitX, final float hitY, final float hitZ) {
2123
if (!worldIn.isRemote) {
2224
TileEntity te = worldIn.getTileEntity(pos);
23-
if (te instanceof MEGasInputBus) {
25+
if (te instanceof MEGasInputBus gasInputBus) {
26+
ItemStack heldItem = playerIn.getHeldItem(hand);
27+
if (heldItem.getItem() instanceof IMemoryCard memoryCard) {
28+
boolean handled = handleSettingsTransfer(gasInputBus, memoryCard, playerIn, heldItem);
29+
if (handled) {
30+
return true;
31+
}
32+
}
33+
2434
playerIn.openGui(ModularMachinery.MODID, CommonProxy.GuiType.ME_GAS_INPUT_BUS.ordinal(), worldIn, pos.getX(), pos.getY(), pos.getZ());
2535
}
2636
}

src/main/java/github/kasuminova/mmce/common/block/appeng/BlockMEItemInputBus.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package github.kasuminova.mmce.common.block.appeng;
22

3+
import appeng.api.implementations.items.IMemoryCard;
34
import github.kasuminova.mmce.common.tile.MEItemInputBus;
45
import hellfirepvp.modularmachinery.ModularMachinery;
56
import hellfirepvp.modularmachinery.common.CommonProxy;
@@ -31,7 +32,15 @@ public boolean onBlockActivated(
3132
{
3233
if (!worldIn.isRemote) {
3334
TileEntity te = worldIn.getTileEntity(pos);
34-
if (te instanceof MEItemInputBus) {
35+
if (te instanceof MEItemInputBus itemInputBus) {
36+
ItemStack heldItem = playerIn.getHeldItem(hand);
37+
if (heldItem.getItem() instanceof IMemoryCard memoryCard) {
38+
boolean handled = handleSettingsTransfer(itemInputBus, memoryCard, playerIn, heldItem);
39+
if (handled) {
40+
return true;
41+
}
42+
}
43+
3544
playerIn.openGui(ModularMachinery.MODID, CommonProxy.GuiType.ME_ITEM_INPUT_BUS.ordinal(), worldIn, pos.getX(), pos.getY(), pos.getZ());
3645
}
3746
}

src/main/java/github/kasuminova/mmce/common/block/appeng/BlockMEMachineComponent.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package github.kasuminova.mmce.common.block.appeng;
22

3+
import appeng.api.implementations.items.IMemoryCard;
4+
import appeng.api.implementations.items.MemoryCardMessages;
5+
import github.kasuminova.mmce.common.tile.SettingsTransfer;
36
import hellfirepvp.modularmachinery.common.CommonProxy;
47
import hellfirepvp.modularmachinery.common.block.BlockMachineComponent;
58
import net.minecraft.block.SoundType;
69
import net.minecraft.block.material.Material;
710
import net.minecraft.block.state.IBlockState;
811
import net.minecraft.entity.player.EntityPlayer;
12+
import net.minecraft.item.ItemStack;
13+
import net.minecraft.nbt.NBTTagCompound;
914
import net.minecraft.tileentity.TileEntity;
1015
import net.minecraft.util.BlockRenderLayer;
1116
import net.minecraft.util.EnumBlockRenderType;
@@ -48,4 +53,25 @@ public EnumBlockRenderType getRenderType(@Nonnull IBlockState state) {
4853
public BlockRenderLayer getRenderLayer() {
4954
return BlockRenderLayer.CUTOUT;
5055
}
56+
57+
protected boolean handleSettingsTransfer(@Nonnull SettingsTransfer settingsProvider, @Nonnull IMemoryCard memoryCard, @Nonnull EntityPlayer player, @Nonnull ItemStack heldItem) {
58+
if (player.isSneaking()) {
59+
NBTTagCompound tag = settingsProvider.downloadSettings();
60+
if (tag != null) {
61+
memoryCard.setMemoryCardContents(heldItem, getTranslationKey(), tag);
62+
return true;
63+
}
64+
} else {
65+
String savedName = memoryCard.getSettingsName(heldItem);
66+
NBTTagCompound tag = memoryCard.getData(heldItem);
67+
if (getTranslationKey().equals(savedName)) {
68+
settingsProvider.uploadSettings(tag);
69+
memoryCard.notifyUser(player, MemoryCardMessages.SETTINGS_LOADED);
70+
return true;
71+
} else {
72+
memoryCard.notifyUser(player, MemoryCardMessages.INVALID_MACHINE);
73+
}
74+
}
75+
return false;
76+
}
5177
}

src/main/java/github/kasuminova/mmce/common/tile/MEFluidInputBus.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import appeng.me.GridAccessException;
1111
import appeng.util.Platform;
1212
import github.kasuminova.mmce.common.tile.base.MEFluidBus;
13+
import hellfirepvp.modularmachinery.ModularMachinery;
1314
import hellfirepvp.modularmachinery.common.lib.ItemsMM;
1415
import hellfirepvp.modularmachinery.common.machine.IOType;
1516
import hellfirepvp.modularmachinery.common.machine.MachineComponent;
@@ -21,7 +22,10 @@
2122
import javax.annotation.Nullable;
2223
import java.util.concurrent.locks.ReadWriteLock;
2324

24-
public class MEFluidInputBus extends MEFluidBus {
25+
public class MEFluidInputBus extends MEFluidBus implements SettingsTransfer {
26+
27+
private static final String CONFIG_TAG_KEY = "config";
28+
2529
private final AEFluidInventory config = new AEFluidInventory(this, MEFluidBus.TANK_SLOT_AMOUNT);
2630

2731
@Override
@@ -32,13 +36,13 @@ public ItemStack getVisualItemStack() {
3236
@Override
3337
public void readCustomNBT(final NBTTagCompound compound) {
3438
super.readCustomNBT(compound);
35-
config.readFromNBT(compound, "config");
39+
config.readFromNBT(compound, CONFIG_TAG_KEY);
3640
}
3741

3842
@Override
3943
public void writeCustomNBT(final NBTTagCompound compound) {
4044
super.writeCustomNBT(compound);
41-
config.writeToNBT(compound, "config");
45+
config.writeToNBT(compound, CONFIG_TAG_KEY);
4246
}
4347

4448
public IAEFluidTank getConfig() {
@@ -198,4 +202,22 @@ public void markNoUpdate() {
198202

199203
super.markNoUpdate();
200204
}
205+
206+
@Override
207+
public NBTTagCompound downloadSettings() {
208+
NBTTagCompound tag = new NBTTagCompound();
209+
config.writeToNBT(tag, CONFIG_TAG_KEY);
210+
return tag;
211+
}
212+
213+
@Override
214+
public void uploadSettings(NBTTagCompound settings) {
215+
config.readFromNBT(settings, CONFIG_TAG_KEY);
216+
this.markForUpdate();
217+
try {
218+
proxy.getTick().alertDevice(proxy.getNode());
219+
} catch (GridAccessException e) {
220+
ModularMachinery.log.warn("Error while uploading settings", e);
221+
}
222+
}
201223
}

src/main/java/github/kasuminova/mmce/common/tile/MEGasInputBus.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.mekeng.github.common.me.inventory.impl.GasInventory;
1212
import github.kasuminova.mmce.common.tile.base.MEGasBus;
1313
import github.kasuminova.mmce.common.util.IExtendedGasHandler;
14+
import hellfirepvp.modularmachinery.ModularMachinery;
1415
import hellfirepvp.modularmachinery.common.crafting.ComponentType;
1516
import hellfirepvp.modularmachinery.common.lib.ComponentTypesMM;
1617
import hellfirepvp.modularmachinery.common.lib.ItemsMM;
@@ -24,7 +25,10 @@
2425
import javax.annotation.Nullable;
2526
import java.util.Objects;
2627

27-
public class MEGasInputBus extends MEGasBus {
28+
public class MEGasInputBus extends MEGasBus implements SettingsTransfer {
29+
30+
private static final String CONFIG_TAG_KEY = "config";
31+
2832
private final GasInventory config = new GasInventory(MEGasBus.TANK_SLOT_AMOUNT, this);
2933

3034
@Override
@@ -35,13 +39,13 @@ public ItemStack getVisualItemStack() {
3539
@Override
3640
public void readCustomNBT(final NBTTagCompound compound) {
3741
super.readCustomNBT(compound);
38-
config.load(compound.getCompoundTag("config"));
42+
config.load(compound.getCompoundTag(CONFIG_TAG_KEY));
3943
}
4044

4145
@Override
4246
public void writeCustomNBT(final NBTTagCompound compound) {
4347
super.writeCustomNBT(compound);
44-
compound.setTag("config", config.save());
48+
compound.setTag(CONFIG_TAG_KEY, config.save());
4549
upgrades.writeToNBT(compound, "upgrades");
4650
}
4751

@@ -206,4 +210,21 @@ public void markNoUpdate() {
206210

207211
super.markNoUpdate();
208212
}
213+
214+
@Override
215+
public NBTTagCompound downloadSettings() {
216+
NBTTagCompound tag = new NBTTagCompound();
217+
tag.setTag(CONFIG_TAG_KEY, config.save());
218+
return tag;
219+
}
220+
221+
@Override
222+
public void uploadSettings(NBTTagCompound settings) {
223+
config.load(settings.getCompoundTag(CONFIG_TAG_KEY));
224+
try {
225+
proxy.getTick().alertDevice(proxy.getNode());
226+
} catch (GridAccessException e) {
227+
ModularMachinery.log.warn("Error while uploading settings", e);
228+
}
229+
}
209230
}

src/main/java/github/kasuminova/mmce/common/tile/MEItemInputBus.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import appeng.me.GridAccessException;
99
import appeng.util.Platform;
1010
import github.kasuminova.mmce.common.tile.base.MEItemBus;
11+
import hellfirepvp.modularmachinery.ModularMachinery;
1112
import hellfirepvp.modularmachinery.common.lib.ItemsMM;
1213
import hellfirepvp.modularmachinery.common.machine.IOType;
1314
import hellfirepvp.modularmachinery.common.machine.MachineComponent;
@@ -22,7 +23,10 @@
2223
import java.util.WeakHashMap;
2324
import java.util.concurrent.locks.ReadWriteLock;
2425

25-
public class MEItemInputBus extends MEItemBus {
26+
public class MEItemInputBus extends MEItemBus implements SettingsTransfer {
27+
28+
private static final String CONFIG_TAG_KEY = "configInventory";
29+
2630
// A simple cache for AEItemStack.
2731
private static final Map<ItemStack, IAEItemStack> AE_STACK_CACHE = new WeakHashMap<>();
2832
private IOInventory configInventory = buildConfigInventory();
@@ -71,16 +75,16 @@ public IOInventory buildConfigInventory() {
7175
public void readCustomNBT(final NBTTagCompound compound) {
7276
super.readCustomNBT(compound);
7377

74-
if (compound.hasKey("configInventory")) {
75-
readConfigInventoryNBT(compound.getCompoundTag("configInventory"));
78+
if (compound.hasKey(CONFIG_TAG_KEY)) {
79+
readConfigInventoryNBT(compound.getCompoundTag(CONFIG_TAG_KEY));
7680
}
7781
}
7882

7983
@Override
8084
public void writeCustomNBT(final NBTTagCompound compound) {
8185
super.writeCustomNBT(compound);
8286

83-
compound.setTag("configInventory", configInventory.writeNBT());
87+
compound.setTag(CONFIG_TAG_KEY, configInventory.writeNBT());
8488
}
8589

8690
public IOInventory getConfigInventory() {
@@ -281,4 +285,21 @@ public void readConfigInventoryNBT(final NBTTagCompound compound) {
281285
}
282286
configInventory.setStackLimit(Integer.MAX_VALUE, slotIDs);
283287
}
288+
289+
@Override
290+
public NBTTagCompound downloadSettings() {
291+
NBTTagCompound tag = new NBTTagCompound();
292+
tag.setTag(CONFIG_TAG_KEY, configInventory.writeNBT());
293+
return tag;
294+
}
295+
296+
@Override
297+
public void uploadSettings(NBTTagCompound settings) {
298+
readConfigInventoryNBT(settings.getCompoundTag(CONFIG_TAG_KEY));
299+
try {
300+
proxy.getTick().alertDevice(proxy.getNode());
301+
} catch (GridAccessException e) {
302+
ModularMachinery.log.warn("Error while uploading settings", e);
303+
}
304+
}
284305
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package github.kasuminova.mmce.common.tile;
2+
3+
import net.minecraft.nbt.NBTTagCompound;
4+
5+
public interface SettingsTransfer {
6+
NBTTagCompound downloadSettings();
7+
void uploadSettings(NBTTagCompound settings);
8+
}

0 commit comments

Comments
 (0)