Skip to content

Commit

Permalink
Permission events should always occur first
Browse files Browse the repository at this point in the history
  • Loading branch information
Thodor12 committed Jan 26, 2025
1 parent 8645a66 commit b1ac4be
Showing 1 changed file with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import net.minecraftforge.event.level.BlockEvent;
import net.minecraftforge.event.level.ExplosionEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -105,7 +106,7 @@ public ColonyPermissionEventHandler(final Colony colony)
* </ul>
* </p>
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final BlockEvent.EntityPlaceEvent event)
{
if (!(event.getEntity() instanceof Player player))
Expand Down Expand Up @@ -180,7 +181,7 @@ private void cancelEvent(final Event event, @Nullable final Entity entity, final
*
* @param event BlockEvent.BreakEvent
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final BlockEvent.BreakEvent event)
{
final LevelAccessor world = event.getLevel();
Expand All @@ -203,7 +204,7 @@ public void on(final BlockEvent.BreakEvent event)
return;
}

if (event.getState().getBlock() == ModBlocks.blockHutTownHall && !((BlockHutTownHall)event.getState().getBlock()).getValidBreak() && !event.getPlayer().isCreative())
if (event.getState().getBlock() == ModBlocks.blockHutTownHall && !((BlockHutTownHall) event.getState().getBlock()).getValidBreak() && !event.getPlayer().isCreative())
{
cancelEvent(event, event.getPlayer(), colony, Action.BREAK_HUTS, event.getPos());
return;
Expand Down Expand Up @@ -243,7 +244,7 @@ else if (event.getState().getBlock() instanceof BlockDecorationController)
*
* @param event ExplosionEvent.Detonate
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final ExplosionEvent.Detonate event)
{
if (MineColonies.getConfig().getServer().turnOffExplosionsInColonies.get() == Explosions.DAMAGE_EVERYTHING)
Expand Down Expand Up @@ -282,7 +283,7 @@ public void on(final ExplosionEvent.Detonate event)
*
* @param event ExplosionEvent.Detonate
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final ExplosionEvent.Start event)
{
if (MineColonies.getConfig().getServer().enableColonyProtection.get()
Expand Down Expand Up @@ -313,7 +314,7 @@ public void on(final ExplosionEvent.Start event)
*
* @param event the event instance.
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final PlayerInteractEvent.RightClickBlock event)
{
if (!MineColonies.getConfig().getServer().enableColonyProtection.get() || !colony.isCoordInColony(event.getLevel(), event.getPos()))
Expand Down Expand Up @@ -385,7 +386,7 @@ else if (isFreeToInteractWith(block, event.getPos()))
*
* @param event the event instance.
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final PlayerInteractEvent.RightClickItem event)
{
if (!MineColonies.getConfig().getServer().enableColonyProtection.get() || !colony.isCoordInColony(event.getLevel(), event.getPos()))
Expand Down Expand Up @@ -416,7 +417,8 @@ else if (stack.getItem() instanceof ItemScanTool)
*/
private boolean isFreeToInteractWith(@Nullable final Block block, final BlockPos pos)
{
return (block != null && (colony.getFreeBlocks().contains(block) || block.defaultBlockState().is(ModTags.colonyProtectionException))) || colony.getFreePositions().contains(pos);
return (block != null && (colony.getFreeBlocks().contains(block) || block.defaultBlockState().is(ModTags.colonyProtectionException))) || colony.getFreePositions()
.contains(pos);
}

/**
Expand All @@ -426,7 +428,7 @@ private boolean isFreeToInteractWith(@Nullable final Block block, final BlockPos
*
* @param event PlayerInteractEvent
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final PlayerInteractEvent.EntityInteract event)
{
if (isFreeToInteractWith(null, event.getPos())
Expand All @@ -453,7 +455,12 @@ public void on(final PlayerInteractEvent.EntityInteract event)
* @param pos the position. Can be null if no target was provided to the event.
* @return true if canceled.
*/
private boolean checkEventCancellation(final Action action, @NotNull final Player playerIn, @NotNull final Level world, @NotNull final Event event, @Nullable final BlockPos pos)
private boolean checkEventCancellation(
final Action action,
@NotNull final Player playerIn,
@NotNull final Level world,
@NotNull final Event event,
@Nullable final BlockPos pos)
{
@NotNull final Player player = EntityUtils.getPlayerOfFakePlayer(playerIn, world);

Expand Down Expand Up @@ -482,7 +489,7 @@ private boolean checkEventCancellation(final Action action, @NotNull final Playe
*
* @param event PlayerInteractEvent
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final PlayerInteractEvent.EntityInteractSpecific event)
{
if (isFreeToInteractWith(null, event.getPos())
Expand All @@ -500,7 +507,7 @@ public void on(final PlayerInteractEvent.EntityInteractSpecific event)
*
* @param event ItemTossEvent
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final ItemTossEvent event)
{
if (checkEventCancellation(Action.TOSS_ITEM, event.getPlayer(), event.getPlayer().getCommandSenderWorld(), event, event.getPlayer().blockPosition()))
Expand All @@ -516,7 +523,7 @@ public void on(final ItemTossEvent event)
*
* @param event ItemEntityPickupEvent
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final EntityItemPickupEvent event)
{
checkEventCancellation(Action.PICKUP_ITEM, event.getEntity(), event.getEntity().getCommandSenderWorld(), event, event.getEntity().blockPosition());
Expand All @@ -529,7 +536,7 @@ public void on(final EntityItemPickupEvent event)
*
* @param event ItemEntityPickupEvent
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final FillBucketEvent event)
{
BlockPos targetBlockPos = null;
Expand All @@ -551,7 +558,7 @@ else if (event.getTarget() instanceof EntityHitResult result)
*
* @param event ItemEntityPickupEvent
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final ArrowLooseEvent event)
{
checkEventCancellation(Action.SHOOT_ARROW, event.getEntity(), event.getEntity().getCommandSenderWorld(), event, event.getEntity().blockPosition());
Expand All @@ -563,9 +570,10 @@ public void on(final ArrowLooseEvent event)
* Check if the entity that is getting hurt is a player,
* players that get hurt by other players are handled elsewhere,
* this here is handling players getting hurt by citizens.
*
* @param event the event instance.
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final LivingHurtEvent event)
{
if (event.getEntity() instanceof ServerPlayer player
Expand All @@ -585,7 +593,7 @@ public void on(final LivingHurtEvent event)
*
* @param event ItemEntityPickupEvent
*/
@SubscribeEvent
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void on(final AttackEntityEvent event)
{
if (event.getTarget() instanceof Monster)
Expand Down

0 comments on commit b1ac4be

Please sign in to comment.