Skip to content

Commit

Permalink
Fixed #2783
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgazul committed Nov 7, 2023
1 parent e03e826 commit ffb61f2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,55 +63,59 @@ public void onBreakBlockEvent(BlockEvent.BreakEvent event) {
@SubscribeEvent(receiveCanceled = true)
public void onBlockPlace(BlockEvent.EntityPlaceEvent event) {
Entity entity = event.getEntity();
if (entity instanceof ServerPlayerEntity) {
ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity;
Player player = ((CraftServer) Bukkit.getServer()).getPlayer(playerEntity);
Direction direction = event.direction;
if (direction != null) {
Hand hand = event.hand == null ? Hand.MAIN_HAND : event.hand;
CraftBlock placedBlock = CraftCustomSnapshot.fromBlockSnapshot(event.getBlockSnapshot(), true);
CraftBlock againstBlock = CraftBlock.at(event.getWorld(), event.getPos().relative(direction.getOpposite()));
ItemStack bukkitStack;
EquipmentSlot bukkitHand;
if (hand == Hand.MAIN_HAND) {
bukkitStack = player.getInventory().getItemInMainHand();
bukkitHand = EquipmentSlot.HAND;
} else {
bukkitStack = player.getInventory().getItemInOffHand();
bukkitHand = EquipmentSlot.OFF_HAND;
if (!event.getWorld().isClientSide() && event.getWorld() instanceof ServerWorld) {
if (entity instanceof ServerPlayerEntity) {
ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity;
Player player = ((CraftServer) Bukkit.getServer()).getPlayer(playerEntity);
Direction direction = event.getPlaceEventDirection();
if (direction != null) {
Hand hand = event.getPlaceEventHand();
CraftBlock placedBlock = CraftCustomSnapshot.fromBlockSnapshot(event.getBlockSnapshot(), true);
CraftBlock againstBlock = CraftBlock.at(event.getWorld(), event.getPos().relative(direction.getOpposite()));
ItemStack bukkitStack;
EquipmentSlot bukkitHand;
if (hand == Hand.MAIN_HAND) {
bukkitStack = player.getInventory().getItemInMainHand();
bukkitHand = EquipmentSlot.HAND;
} else {
bukkitStack = player.getInventory().getItemInOffHand();
bukkitHand = EquipmentSlot.OFF_HAND;
}
BlockPlaceEvent placeEvent = new BlockPlaceEvent(placedBlock, placedBlock.getState(), againstBlock, bukkitStack, player, !event.isCanceled(), bukkitHand);
placeEvent.setCancelled(event.isCanceled());
Bukkit.getPluginManager().callEvent(placeEvent);
event.setCanceled(placeEvent.isCancelled() || !placeEvent.canBuild());
}
BlockPlaceEvent placeEvent = new BlockPlaceEvent(placedBlock, placedBlock.getState(), againstBlock, bukkitStack, player, !event.isCanceled(), bukkitHand);
placeEvent.setCancelled(event.isCanceled());
Bukkit.getPluginManager().callEvent(placeEvent);
event.setCanceled(placeEvent.isCancelled() || !placeEvent.canBuild());
}
}
}

@SubscribeEvent(receiveCanceled = true)
public void onMultiPlace(BlockEvent.EntityMultiPlaceEvent event) {
Entity entity = event.getEntity();
if (entity instanceof ServerPlayerEntity) {
ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity;
Player player = ((CraftServer) Bukkit.getServer()).getPlayer(playerEntity);
Direction direction = event.direction;
if (direction != null) {
Hand hand = event.hand == null ? Hand.MAIN_HAND : event.hand;
List<BlockState> placedBlocks = new ArrayList<>(event.getReplacedBlockSnapshots().size());
for (BlockSnapshot snapshot : event.getReplacedBlockSnapshots()) {
placedBlocks.add(CraftCustomSnapshot.fromBlockSnapshot(snapshot, true).getState());
}
CraftBlock againstBlock = CraftBlock.at(event.getWorld(), event.getPos().relative(direction.getOpposite()));
ItemStack bukkitStack;
if (hand == Hand.MAIN_HAND) {
bukkitStack = player.getInventory().getItemInMainHand();
} else {
bukkitStack = player.getInventory().getItemInOffHand();
if (!event.getWorld().isClientSide() && event.getWorld() instanceof ServerWorld) {
if (entity instanceof ServerPlayerEntity) {
ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity;
Player player = ((CraftServer) Bukkit.getServer()).getPlayer(playerEntity);
Direction direction = event.getPlaceEventDirection();
if (direction != null) {
Hand hand = event.getPlaceEventHand();
List<BlockState> placedBlocks = new ArrayList<>(event.getReplacedBlockSnapshots().size());
for (BlockSnapshot snapshot : event.getReplacedBlockSnapshots()) {
placedBlocks.add(CraftCustomSnapshot.fromBlockSnapshot(snapshot, true).getState());
}
CraftBlock againstBlock = CraftBlock.at(event.getWorld(), event.getPos().relative(direction.getOpposite()));
ItemStack bukkitStack;
if (hand == Hand.MAIN_HAND) {
bukkitStack = player.getInventory().getItemInMainHand();
} else {
bukkitStack = player.getInventory().getItemInOffHand();
}
BlockPlaceEvent placeEvent = new BlockMultiPlaceEvent(placedBlocks, againstBlock, bukkitStack, player, !event.isCanceled());
placeEvent.setCancelled(event.isCanceled());
Bukkit.getPluginManager().callEvent(placeEvent);
event.setCanceled(placeEvent.isCancelled() || !placeEvent.canBuild());
}
BlockPlaceEvent placeEvent = new BlockMultiPlaceEvent(placedBlocks, againstBlock, bukkitStack, player, !event.isCanceled());
placeEvent.setCancelled(event.isCanceled());
Bukkit.getPluginManager().callEvent(placeEvent);
event.setCanceled(placeEvent.isCancelled() || !placeEvent.canBuild());
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/net/minecraftforge/common/ForgeHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@ public static int onBlockBreakEvent(World world, GameType gameType, ServerPlayer

public static ActionResultType onPlaceItemIntoWorld(@Nonnull ItemUseContext context)
{
EntityPlaceEvent.hand = context.getHand();
ItemStack itemstack = context.getItemInHand();
World world = context.getLevel();

Expand Down Expand Up @@ -668,15 +667,16 @@ public static ActionResultType onPlaceItemIntoWorld(@Nonnull ItemUseContext cont
itemstack.setTag(nbt);

Direction side = context.getClickedFace();
Hand hand = context.getHand(); // Mohist

boolean eventResult = false;
if (blockSnapshots.size() > 1)
{
eventResult = ForgeEventFactory.onMultiBlockPlace(player, blockSnapshots, side);
eventResult = ForgeEventFactory.onMultiBlockPlace(player, blockSnapshots, side, hand); // Mohist add hand
}
else if (blockSnapshots.size() == 1)
{
eventResult = ForgeEventFactory.onBlockPlace(player, blockSnapshots.get(0), side);
eventResult = ForgeEventFactory.onBlockPlace(player, blockSnapshots.get(0), side, hand); // Mohist add hand
}

if (eventResult)
Expand Down Expand Up @@ -709,7 +709,6 @@ else if (blockSnapshots.size() == 1)
}
}
world.capturedBlockSnapshots.clear();
EntityPlaceEvent.hand = Hand.MAIN_HAND;
return ret;
}

Expand Down
20 changes: 19 additions & 1 deletion src/main/java/net/minecraftforge/event/ForgeEventFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,29 @@ public static boolean onMultiBlockPlace(@Nullable Entity entity, List<BlockSnaps
return MinecraftForge.EVENT_BUS.post(event);
}

public static boolean onMultiBlockPlace(@Nullable Entity entity, List<BlockSnapshot> blockSnapshots, Direction direction, Hand hand)
{
BlockSnapshot snap = blockSnapshots.get(0);
BlockState placedAgainst = snap.getWorld().getBlockState(snap.getPos().relative(direction.getOpposite()));
EntityMultiPlaceEvent event = new EntityMultiPlaceEvent(blockSnapshots, placedAgainst, entity);
event.setPlaceEventDirection(direction); // Mohist
event.setPlaceEventHand(hand); // Mohist
return MinecraftForge.EVENT_BUS.post(event);
}

public static boolean onBlockPlace(@Nullable Entity entity, @Nonnull BlockSnapshot blockSnapshot, @Nonnull Direction direction)
{
BlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().relative(direction.getOpposite()));
EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, entity);
event.direction = direction;
return MinecraftForge.EVENT_BUS.post(event);
}

public static boolean onBlockPlace(@Nullable Entity entity, @Nonnull BlockSnapshot blockSnapshot, @Nonnull Direction direction, Hand hand)
{
BlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().relative(direction.getOpposite()));
EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, entity);
event.setPlaceEventDirection(direction); // Mohist
event.setPlaceEventHand(hand); // Mohist
return MinecraftForge.EVENT_BUS.post(event);
}

Expand Down
18 changes: 16 additions & 2 deletions src/main/java/net/minecraftforge/event/world/BlockEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public static class EntityPlaceEvent extends BlockEvent
private final BlockState placedBlock;
private final BlockState placedAgainst;
// Mohist start
public static Direction direction = null;
public static Hand hand = null;
public static Direction placeEventDirection = null;
public static Hand placeEventHand = Hand.MAIN_HAND;
// Mohist end

public EntityPlaceEvent(@Nonnull BlockSnapshot blockSnapshot, @Nonnull BlockState placedAgainst, @Nullable Entity entity)
Expand All @@ -155,6 +155,20 @@ public EntityPlaceEvent(@Nonnull BlockSnapshot blockSnapshot, @Nonnull BlockStat
public BlockSnapshot getBlockSnapshot() { return blockSnapshot; }
public BlockState getPlacedBlock() { return placedBlock; }
public BlockState getPlacedAgainst() { return placedAgainst; }
// Mohist start
public void setPlaceEventDirection(Direction placeEventDirection) {
this.placeEventDirection = placeEventDirection;
}
public Direction getPlaceEventDirection() {
return placeEventDirection;
}
public void setPlaceEventHand(Hand placeEventHand) {
this.placeEventHand = placeEventHand;
}
public Hand getPlaceEventHand() {
return placeEventHand;
}
// Mohist end
}

/**
Expand Down

0 comments on commit ffb61f2

Please sign in to comment.