Skip to content

Commit dfad691

Browse files
authored
Merge pull request #136 from xinyihl/master
修改控制器保存 nbt 到物品实体的逻辑
2 parents b6022bb + a9f012c commit dfad691

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

src/main/java/hellfirepvp/modularmachinery/common/block/BlockController.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import net.minecraft.util.*;
3737
import net.minecraft.util.math.AxisAlignedBB;
3838
import net.minecraft.util.math.BlockPos;
39+
import net.minecraft.util.math.RayTraceResult;
3940
import net.minecraft.world.IBlockAccess;
4041
import net.minecraft.world.World;
4142
import net.minecraftforge.fml.relauncher.Side;
@@ -111,27 +112,30 @@ public void addInformation(ItemStack stack, @Nullable World worldIn, List<String
111112
}
112113

113114
@Override
114-
public void dropBlockAsItemWithChance(@Nonnull final World worldIn, @Nonnull final BlockPos pos, @Nonnull final IBlockState state, final float chance, final int fortune) {
115+
public void getDrops(@Nonnull NonNullList<ItemStack> result, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull IBlockState metadata, int fortune) {
116+
ItemStack stack = getRestorableDropItem(world, pos, metadata);
117+
if (stack != null && !stack.isEmpty()) {
118+
result.add(stack);
119+
} else {
120+
super.getDrops(result, world, pos, metadata, fortune);
121+
}
115122
}
116123

124+
@Nonnull
117125
@Override
118-
public void getDrops(@Nonnull final NonNullList<ItemStack> drops, @Nonnull final IBlockAccess world, @Nonnull final BlockPos pos, @Nonnull final IBlockState state, final int fortune) {
126+
public ItemStack getPickBlock(@Nonnull IBlockState state, @Nonnull RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EntityPlayer player) {
127+
ItemStack stack = getRestorableDropItem(world, pos, state);
128+
if (stack != null && !stack.isEmpty()) {
129+
return stack;
130+
} else {
131+
return super.getPickBlock(state, target, world, pos, player);
132+
}
119133
}
120134

121-
@Override
122-
public void breakBlock(World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
123-
Random rand = worldIn.rand;
124-
TileEntity te = worldIn.getTileEntity(pos);
125-
if (te instanceof TileMultiblockMachineController ctrl) {
126-
IOInventory inv = ctrl.getInventory();
127-
for (int i = 0; i < inv.getSlots(); i++) {
128-
ItemStack stack = inv.getStackInSlot(i);
129-
if (!stack.isEmpty()) {
130-
spawnAsEntity(worldIn, pos, stack);
131-
inv.setStackInSlot(i, ItemStack.EMPTY);
132-
}
133-
}
134-
135+
private ItemStack getRestorableDropItem(@Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
136+
Random rand = world instanceof World ? ((World)world).rand : RANDOM;
137+
TileEntity tileEntity = world.getTileEntity(pos);
138+
if (tileEntity instanceof TileMultiblockMachineController ctrl && ctrl.getOwner() != null) {
135139
UUID ownerUUID = ctrl.getOwner();
136140
Item dropped = getItemDropped(state, rand, damageDropped(state));
137141
if (dropped instanceof ItemBlockController) {
@@ -141,12 +145,38 @@ public void breakBlock(World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockStat
141145
tag.setString("owner", ownerUUID.toString());
142146
stackCtrl.setTagCompound(tag);
143147
}
144-
spawnAsEntity(worldIn, pos, stackCtrl);
148+
return stackCtrl;
145149
} else {
146-
ModularMachinery.log.warn("Cannot get controller drops at World: " + worldIn + ", Pos: " + MiscUtils.posToString(pos));
150+
ModularMachinery.log.warn("Cannot get controller drops at World: " + world + ", Pos: " + MiscUtils.posToString(pos));
147151
}
148152
}
153+
return null;
154+
}
155+
156+
@Override
157+
public boolean removedByPlayer(@Nonnull IBlockState state, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EntityPlayer player, boolean willHarvest) {
158+
return willHarvest || super.removedByPlayer(state, world, pos, player, willHarvest);
159+
}
160+
161+
@Override
162+
public void harvestBlock(@Nonnull World world, @Nonnull EntityPlayer player, @Nonnull BlockPos pos, @Nonnull IBlockState state, TileEntity te, @Nonnull ItemStack stack) {
163+
super.harvestBlock(world, player, pos, state, te, stack);
164+
world.setBlockToAir(pos);
165+
}
149166

167+
@Override
168+
public void breakBlock(World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
169+
TileEntity te = worldIn.getTileEntity(pos);
170+
if (te instanceof TileMultiblockMachineController ctrl) {
171+
IOInventory inv = ctrl.getInventory();
172+
for (int i = 0; i < inv.getSlots(); i++) {
173+
ItemStack stack = inv.getStackInSlot(i);
174+
if (!stack.isEmpty()) {
175+
spawnAsEntity(worldIn, pos, stack);
176+
inv.setStackInSlot(i, ItemStack.EMPTY);
177+
}
178+
}
179+
}
150180
super.breakBlock(worldIn, pos, state);
151181
}
152182

0 commit comments

Comments
 (0)