Skip to content

Commit 9b7dc9a

Browse files
authored
Merge pull request #2136 from Saereth/block_protection
Adds block protection for all BM world interaction
2 parents f5858cb + 60a6577 commit 9b7dc9a

31 files changed

Lines changed: 806 additions & 88 deletions

src/main/java/wayoftime/bloodmagic/common/block/BlockShapedExplosive.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
import net.minecraft.core.BlockPos;
66
import net.minecraft.core.Direction;
7+
import net.minecraft.world.entity.LivingEntity;
78
import net.minecraft.world.entity.player.Player;
9+
import net.minecraft.world.item.ItemStack;
810
import net.minecraft.world.item.context.BlockPlaceContext;
911
import net.minecraft.world.level.BlockGetter;
1012
import net.minecraft.world.level.Level;
@@ -116,6 +118,20 @@ public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos,
116118
}
117119
}
118120

121+
@Override
122+
public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack)
123+
{
124+
super.setPlacedBy(level, pos, state, placer, stack);
125+
if (placer instanceof Player player)
126+
{
127+
BlockEntity tile = level.getBlockEntity(pos);
128+
if (tile instanceof TileExplosiveCharge explosiveCharge)
129+
{
130+
explosiveCharge.setOwnerUUID(player.getUUID());
131+
}
132+
}
133+
}
134+
119135
@Override
120136
public void playerWillDestroy(Level world, BlockPos blockPos, BlockState blockState, Player player)
121137
{

src/main/java/wayoftime/bloodmagic/common/item/sigil/ItemSigilBloodLight.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import wayoftime.bloodmagic.core.data.SoulTicket;
1616
import wayoftime.bloodmagic.entity.projectile.EntityBloodLight;
1717
import wayoftime.bloodmagic.util.Constants;
18+
import wayoftime.bloodmagic.util.helper.BlockProtectionHelper;
1819
import wayoftime.bloodmagic.util.helper.NBTHelper;
1920
import wayoftime.bloodmagic.util.helper.NetworkHelper;
2021
import wayoftime.bloodmagic.util.helper.PlayerHelper;
@@ -61,14 +62,16 @@ public InteractionResultHolder<ItemStack> use(Level world, Player player, Intera
6162

6263
if (world.isEmptyBlock(blockPos))
6364
{
64-
world.setBlockAndUpdate(blockPos, BloodMagicBlocks.BLOOD_LIGHT.get().defaultBlockState());
65-
if (!world.isClientSide)
65+
if (BlockProtectionHelper.tryPlaceBlock(world, blockPos, BloodMagicBlocks.BLOOD_LIGHT.get().defaultBlockState(), player))
6666
{
67-
SoulNetwork network = NetworkHelper.getSoulNetwork(getBinding(stack));
68-
network.syphonAndDamage(player, SoulTicket.item(stack, world, player, getLpUsed()));
67+
if (!world.isClientSide)
68+
{
69+
SoulNetwork network = NetworkHelper.getSoulNetwork(getBinding(stack));
70+
network.syphonAndDamage(player, SoulTicket.item(stack, world, player, getLpUsed()));
71+
}
72+
resetCooldown(stack);
73+
player.swing(hand);
6974
}
70-
resetCooldown(stack);
71-
player.swing(hand);
7275
return super.use(world, player, hand);
7376
}
7477
} else

src/main/java/wayoftime/bloodmagic/common/item/sigil/ItemSigilFluidBase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import net.minecraftforge.fluids.capability.IFluidHandler;
1414
import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction;
1515
import net.minecraftforge.fluids.capability.wrappers.BlockWrapper;
16+
import wayoftime.bloodmagic.util.helper.BlockProtectionHelper;
1617

1718
public abstract class ItemSigilFluidBase extends ItemSigilBase
1819
{
@@ -101,6 +102,13 @@ protected boolean tryPlaceSigilFluid(Player player, Level world, BlockPos blockP
101102
{
102103
FluidStack resource = sigilFluid;
103104
BlockState state = sigilFluid.getFluid().getFluidType().getBlockForFluidState(world, blockPos, sigilFluid.getFluid().defaultFluidState());
105+
106+
// Check protection before placing fluid
107+
if (!BlockProtectionHelper.canPlaceBlock(world, blockPos, player))
108+
{
109+
return false;
110+
}
111+
104112
BlockWrapper wrapper = new BlockWrapper(state, world, blockPos);
105113

106114
if (world.dimensionType().ultraWarm() && resource.getFluid().getFluidType().isVaporizedOnPlacement(world, blockPos, resource))

src/main/java/wayoftime/bloodmagic/common/tile/TileDeforesterCharge.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
2929
import net.minecraft.world.phys.Vec3;
3030
import wayoftime.bloodmagic.common.block.BlockShapedExplosive;
31+
import wayoftime.bloodmagic.util.helper.BlockProtectionHelper;
3132

3233
public class TileDeforesterCharge extends TileExplosiveCharge
3334
{
@@ -191,6 +192,13 @@ public void onUpdate()
191192
// this.world.getProfiler().startSection("explosion_blocks");
192193
if (this.level instanceof ServerLevel)
193194
{
195+
// Check protection before breaking - use strict mode to prevent breaking
196+
// if owner is unknown (e.g., placed by dispenser) or offline
197+
if (!BlockProtectionHelper.canBreakBlockStrict(level, blockPos, getOwnerUUID()))
198+
{
199+
continue;
200+
}
201+
194202
BlockEntity tileentity = blockstate.getBlock() instanceof EntityBlock
195203
? this.level.getBlockEntity(blockPos)
196204
: null;

src/main/java/wayoftime/bloodmagic/common/tile/TileExplosiveCharge.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
import wayoftime.bloodmagic.anointment.AnointmentHolder;
1515
import wayoftime.bloodmagic.common.tile.base.TileTicking;
1616

17+
import javax.annotation.Nullable;
18+
import java.util.UUID;
19+
1720
public class TileExplosiveCharge extends TileTicking
1821
{
1922
public AnointmentHolder anointmentHolder = new AnointmentHolder();
23+
@Nullable
24+
protected UUID ownerUUID = null;
2025

2126
public TileExplosiveCharge(BlockEntityType<?> type, BlockPos pos, BlockState state)
2227
{
@@ -60,6 +65,10 @@ public void deserialize(CompoundTag tag)
6065
{
6166
anointmentHolder = AnointmentHolder.fromNBT(tag.getCompound("holder"));
6267
}
68+
if (tag.hasUUID("ownerUUID"))
69+
{
70+
ownerUUID = tag.getUUID("ownerUUID");
71+
}
6372

6473
}
6574

@@ -70,6 +79,10 @@ public CompoundTag serialize(CompoundTag tag)
7079
{
7180
tag.put("holder", anointmentHolder.serialize());
7281
}
82+
if (ownerUUID != null)
83+
{
84+
tag.putUUID("ownerUUID", ownerUUID);
85+
}
7386

7487
return tag;
7588
}
@@ -79,6 +92,17 @@ public void setAnointmentHolder(AnointmentHolder holder)
7992
this.anointmentHolder = holder;
8093
}
8194

95+
@Nullable
96+
public UUID getOwnerUUID()
97+
{
98+
return ownerUUID;
99+
}
100+
101+
public void setOwnerUUID(@Nullable UUID ownerUUID)
102+
{
103+
this.ownerUUID = ownerUUID;
104+
}
105+
82106
public void dropSelf()
83107
{
84108
ItemStack stack = new ItemStack(getBlockState().getBlock());

src/main/java/wayoftime/bloodmagic/common/tile/TileShapedExplosive.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
2424
import net.minecraft.world.phys.Vec3;
2525
import wayoftime.bloodmagic.common.block.BlockShapedExplosive;
26+
import wayoftime.bloodmagic.util.helper.BlockProtectionHelper;
2627

2728
public class TileShapedExplosive extends TileExplosiveCharge
2829
{
@@ -131,6 +132,13 @@ public void onUpdate()
131132
// this.world.getProfiler().startSection("explosion_blocks");
132133
if (this.level instanceof ServerLevel)
133134
{
135+
// Check protection before breaking - use strict mode to prevent breaking
136+
// if owner is unknown (e.g., placed by dispenser) or offline
137+
if (!BlockProtectionHelper.canBreakBlockStrict(level, blockpos, getOwnerUUID()))
138+
{
139+
continue;
140+
}
141+
134142
BlockEntity tileentity = blockstate.getBlock() instanceof EntityBlock
135143
? this.level.getBlockEntity(blockpos)
136144
: null;

src/main/java/wayoftime/bloodmagic/common/tile/TileVeinMineCharge.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
3030
import net.minecraft.world.phys.Vec3;
3131
import wayoftime.bloodmagic.common.block.BlockShapedExplosive;
32+
import wayoftime.bloodmagic.util.helper.BlockProtectionHelper;
3233

3334
public class TileVeinMineCharge extends TileExplosiveCharge
3435
{
@@ -229,6 +230,13 @@ public void onUpdate()
229230
// this.world.getProfiler().startSection("explosion_blocks");
230231
if (this.level instanceof ServerLevel)
231232
{
233+
// Check protection before breaking - use strict mode to prevent breaking
234+
// if owner is unknown (e.g., placed by dispenser) or offline
235+
if (!BlockProtectionHelper.canBreakBlockStrict(level, blockPos, getOwnerUUID()))
236+
{
237+
continue;
238+
}
239+
232240
BlockEntity tileentity = blockstate.getBlock() instanceof EntityBlock
233241
? this.level.getBlockEntity(blockPos)
234242
: null;

src/main/java/wayoftime/bloodmagic/entity/projectile/EntityBloodLight.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import wayoftime.bloodmagic.common.block.BloodMagicBlocks;
2424
import wayoftime.bloodmagic.common.item.BloodMagicItems;
2525
import wayoftime.bloodmagic.common.registries.BloodMagicEntityTypes;
26+
import wayoftime.bloodmagic.util.helper.BlockProtectionHelper;
2627

2728
public class EntityBloodLight extends ThrowableItemProjectile
2829
{
@@ -64,8 +65,11 @@ public void tick()
6465
BlockState blockstate = this.level().getBlockState(blockpos);
6566
if (blockstate.isAir() || blockstate.is(BlockTags.FIRE) || blockstate.canBeReplaced() || blockstate.liquid())
6667
{
67-
this.getCommandSenderWorld().setBlockAndUpdate(blockpos, BloodMagicBlocks.BLOOD_LIGHT.get().defaultBlockState());
68-
this.removeAfterChangingDimensions();
68+
// Check block protection before placing
69+
if (BlockProtectionHelper.tryPlaceBlock(this.level(), blockpos, BloodMagicBlocks.BLOOD_LIGHT.get().defaultBlockState(), this.getOwner()))
70+
{
71+
this.removeAfterChangingDimensions();
72+
}
6973
}
7074
}
7175
}

src/main/java/wayoftime/bloodmagic/entity/projectile/EntityPotionFlask.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import net.minecraftforge.api.distmarker.OnlyIn;
3434
import net.minecraftforge.network.NetworkHooks;
3535

36+
import wayoftime.bloodmagic.util.helper.BlockProtectionHelper;
37+
3638
import javax.annotation.Nullable;
3739
import java.util.List;
3840
import java.util.function.Predicate;
@@ -240,12 +242,20 @@ public void extinguishFires(BlockPos pos, Direction p_184542_2_)
240242
BlockState blockstate = this.level().getBlockState(pos);
241243
if (blockstate.is(BlockTags.FIRE))
242244
{
243-
this.level().removeBlock(pos, false);
245+
// Check protection before removing fire
246+
if (BlockProtectionHelper.tryRemoveBlock(this.level(), pos, this.getOwner()))
247+
{
248+
// Block already removed by tryRemoveBlock
249+
}
244250
} else if (CampfireBlock.isLitCampfire(blockstate))
245251
{
246-
this.level().levelEvent((Player) null, 1009, pos, 0);
247-
CampfireBlock.dowse(null, this.level(), pos, blockstate);
248-
this.level().setBlockAndUpdate(pos, blockstate.setValue(CampfireBlock.LIT, Boolean.valueOf(false)));
252+
// Check protection before modifying campfire
253+
if (BlockProtectionHelper.canPlaceBlock(this.level(), pos, this.getOwner()))
254+
{
255+
this.level().levelEvent((Player) null, 1009, pos, 0);
256+
CampfireBlock.dowse(null, this.level(), pos, blockstate);
257+
this.level().setBlockAndUpdate(pos, blockstate.setValue(CampfireBlock.LIT, Boolean.valueOf(false)));
258+
}
249259
}
250260

251261
}

src/main/java/wayoftime/bloodmagic/entity/projectile/EntityShapedCharge.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import net.minecraft.network.syncher.EntityDataSerializers;
1212
import net.minecraft.network.syncher.SynchedEntityData;
1313
import net.minecraft.tags.BlockTags;
14+
import net.minecraft.world.entity.Entity;
1415
import net.minecraft.world.entity.EntityType;
1516
import net.minecraft.world.entity.LivingEntity;
17+
import net.minecraft.world.entity.player.Player;
1618
import net.minecraft.world.entity.projectile.ProjectileUtil;
1719
import net.minecraft.world.entity.projectile.ThrowableProjectile;
1820
import net.minecraft.world.level.Level;
@@ -30,6 +32,7 @@
3032
import wayoftime.bloodmagic.common.block.BloodMagicBlocks;
3133
import wayoftime.bloodmagic.common.registries.BloodMagicEntityTypes;
3234
import wayoftime.bloodmagic.common.tile.TileExplosiveCharge;
35+
import wayoftime.bloodmagic.util.helper.BlockProtectionHelper;
3336

3437
public class EntityShapedCharge extends ThrowableProjectile
3538
{
@@ -85,13 +88,27 @@ public void tick()
8588
BlockState fallTile = this.getBlockState();
8689
if (blockstate.isAir() || blockstate.is(BlockTags.FIRE) || blockstate.liquid() || blockstate.canBeReplaced())
8790
{
88-
this.getCommandSenderWorld().setBlockAndUpdate(blockpos, fallTile.setValue(BlockShapedExplosive.ATTACHED, faceHit));
89-
BlockEntity tile = this.getCommandSenderWorld().getBlockEntity(blockpos);
90-
if (tile instanceof TileExplosiveCharge)
91+
// Check block protection before placing
92+
if (BlockProtectionHelper.tryPlaceBlock(this.level(), blockpos, fallTile.setValue(BlockShapedExplosive.ATTACHED, faceHit), this.getOwner()))
9193
{
92-
((TileExplosiveCharge) tile).setAnointmentHolder(holder);
94+
BlockEntity tile = this.getCommandSenderWorld().getBlockEntity(blockpos);
95+
if (tile instanceof TileExplosiveCharge explosiveCharge)
96+
{
97+
explosiveCharge.setAnointmentHolder(holder);
98+
// Set the owner from the entity that threw this projectile
99+
Entity owner = this.getOwner();
100+
if (owner instanceof Player player)
101+
{
102+
explosiveCharge.setOwnerUUID(player.getUUID());
103+
}
104+
}
105+
this.removeAfterChangingDimensions();
106+
} else
107+
{
108+
// Protection prevented placement, drop the item
109+
this.spawnAtLocation(fallTile.getBlock());
110+
this.removeAfterChangingDimensions();
93111
}
94-
this.removeAfterChangingDimensions();
95112
} else
96113
{
97114
// BlockItem d;

0 commit comments

Comments
 (0)