Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.18] Fix block duplication due to separate Fabric BlockPlace and BlockRightClick events #213

Open
wants to merge 7 commits into
base: 1.18/dev
Choose a base branch
from
25 changes: 12 additions & 13 deletions common/src/main/java/dev/ftb/mods/ftbchunks/FTBChunks.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public FTBChunks() {
ExplosionEvent.DETONATE.register(this::explosionDetonate);
EntityEvent.LIVING_DEATH.register(this::playerDeath); // LOWEST
CommandRegistrationEvent.EVENT.register(FTBChunksCommands::registerCommands);
TeamEvent.COLLECT_PROPERTIES.register(this::teamConfig);
TeamEvent.COLLECT_PROPERTIES.register(FTBChunksExpected::getTeamConfigsForPlatform);
TeamEvent.PLAYER_JOINED_PARTY.register(this::playerJoinedParty);
TeamEvent.OWNERSHIP_TRANSFERRED.register(this::teamOwnershipTransferred);

Expand Down Expand Up @@ -252,8 +252,11 @@ public EventResult blockLeftClick(Player player, InteractionHand hand, BlockPos
return EventResult.pass();
}

/*
* Architectury fabric uses UseBlockCallback for both place and interact.
*/
public EventResult blockRightClick(Player player, InteractionHand hand, BlockPos pos, Direction face) {
if (player instanceof ServerPlayer && FTBChunksAPI.getManager().protect(player, hand, pos, Protection.INTERACT_BLOCK)) {
if (player instanceof ServerPlayer && FTBChunksAPI.getManager().protect(player, hand, pos, FTBChunksExpected.getBlockInteractProtectionForPlatform())) {
return EventResult.interruptFalse();
}

Expand All @@ -269,15 +272,19 @@ public CompoundEventResult<ItemStack> itemRightClick(Player player, InteractionH
}

public EventResult blockBreak(Level level, BlockPos pos, BlockState blockState, ServerPlayer player, @Nullable IntValue intValue) {
if (FTBChunksAPI.getManager().protect(player, InteractionHand.MAIN_HAND, pos, Protection.EDIT_BLOCK)) {
if (FTBChunksAPI.getManager().protect(player, InteractionHand.MAIN_HAND, pos, FTBChunksExpected.getBlockBreakProtectionForPlatform())) {
return EventResult.interruptFalse();
}

return EventResult.pass();
}

/*
* This is only called on Forge implementations, since architectury fabric does not have an onBlockPlaced event to override;
* architectury fabric uses UseBlockCallback for both place and interact.
*/
public EventResult blockPlace(Level level, BlockPos pos, BlockState blockState, @Nullable Entity entity) {
if (entity instanceof ServerPlayer && FTBChunksAPI.getManager().protect(entity, InteractionHand.MAIN_HAND, pos, Protection.EDIT_BLOCK)) {
if (entity instanceof ServerPlayer && FTBChunksAPI.getManager().protect(entity, InteractionHand.MAIN_HAND, pos, FTBChunksExpected.getBlockPlaceProtectionForPlatform())) {
return EventResult.interruptFalse();
}

Expand Down Expand Up @@ -399,14 +406,6 @@ public EventResult playerDeath(LivingEntity entity, DamageSource source) {
return EventResult.pass();
}

private void teamConfig(TeamCollectPropertiesEvent event) {
event.add(FTBChunksTeamData.ALLOW_FAKE_PLAYERS);
event.add(FTBChunksTeamData.BLOCK_EDIT_MODE);
event.add(FTBChunksTeamData.BLOCK_INTERACT_MODE);
// event.add(FTBChunksTeamData.MINIMAP_MODE);
// event.add(FTBChunksTeamData.LOCATION_MODE);
}

private void playerJoinedParty(PlayerJoinedPartyTeamEvent event) {
CommandSourceStack sourceStack = event.getTeam().manager.server.createCommandSourceStack();
FTBChunksTeamData oldData = FTBChunksAPI.getManager().getData(event.getPreviousTeam());
Expand Down Expand Up @@ -459,4 +458,4 @@ private void teamOwnershipTransferred(PlayerTransferredTeamOwnershipEvent event)
FTBChunksTeamData data = FTBChunksAPI.getManager().getData(event.getTeam());
data.updateLimits(event.getTo());
}
}
}
22 changes: 22 additions & 0 deletions common/src/main/java/dev/ftb/mods/ftbchunks/FTBChunksExpected.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.ftb.mods.ftbchunks;

import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.ftb.mods.ftbchunks.data.Protection;
import dev.ftb.mods.ftbteams.event.TeamCollectPropertiesEvent;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;

Expand All @@ -11,4 +13,24 @@ public class FTBChunksExpected {
public static void addChunkToForceLoaded(ServerLevel level, String modId, UUID owner, int chunkX, int chunkY, boolean add) {
throw new AssertionError();
}

@ExpectPlatform
public static void getTeamConfigsForPlatform(TeamCollectPropertiesEvent event) {
throw new AssertionError();
}

@ExpectPlatform
public static Protection getBlockPlaceProtectionForPlatform() {
throw new AssertionError();
}

@ExpectPlatform
public static Protection getBlockInteractProtectionForPlatform() {
throw new AssertionError();
}

@ExpectPlatform
public static Protection getBlockBreakProtectionForPlatform() {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,12 @@ public boolean protect(@Nullable Entity entity, InteractionHand hand, BlockPos p

if (override.isOverride()) {
return override.getProtect();
} else if (!isFake && getBypassProtection(player.getUUID())) {
return false;
}

return isFake || !getBypassProtection(player.getUUID());
player.displayClientMessage(new TextComponent("You do not have permission to interact with blocks here!"), true);
return true;
} else if (FTBChunksWorldConfig.noWilderness(player)) {
ProtectionOverride override = protection.override(player, pos, hand, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import dev.ftb.mods.ftbteams.FTBTeamsAPI;
import dev.ftb.mods.ftbteams.data.PrivacyMode;
import dev.ftb.mods.ftbteams.data.Team;
import dev.ftb.mods.ftbteams.data.TeamRank;
import dev.ftb.mods.ftbteams.property.BooleanProperty;
import dev.ftb.mods.ftbteams.property.PrivacyProperty;
import net.minecraft.Util;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class FTBChunksTeamData {
public static final BooleanProperty ALLOW_FAKE_PLAYERS = new BooleanProperty(new ResourceLocation(FTBChunks.MOD_ID, "allow_fake_players"), true);
public static final PrivacyProperty BLOCK_EDIT_MODE = new PrivacyProperty(new ResourceLocation(FTBChunks.MOD_ID, "block_edit_mode"), PrivacyMode.ALLIES);
public static final PrivacyProperty BLOCK_INTERACT_MODE = new PrivacyProperty(new ResourceLocation(FTBChunks.MOD_ID, "block_interact_mode"), PrivacyMode.ALLIES);
public static final PrivacyProperty BLOCK_EDIT_AND_INTERACT_MODE = new PrivacyProperty(new ResourceLocation(FTBChunks.MOD_ID, "block_edit_and_interact_mode"), PrivacyMode.ALLIES);
public static final PrivacyProperty MINIMAP_MODE = new PrivacyProperty(new ResourceLocation(FTBChunks.MOD_ID, "minimap_mode"), PrivacyMode.ALLIES);
public static final PrivacyProperty LOCATION_MODE = new PrivacyProperty(new ResourceLocation(FTBChunks.MOD_ID, "location_mode"), PrivacyMode.ALLIES);

Expand Down Expand Up @@ -276,6 +278,8 @@ public boolean canUse(ServerPlayer p, PrivacyProperty property) {
}

return isAlly(p.getUUID());
} else if (mode == PrivacyMode.PRIVATE) {
return team.getRanked(TeamRank.OWNER).containsKey(p.getUUID());
}

return team.isMember(p.getUUID());
Expand Down
16 changes: 15 additions & 1 deletion common/src/main/java/dev/ftb/mods/ftbchunks/data/Protection.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

@FunctionalInterface
public interface Protection {

Protection EDIT_AND_INTERACT_BLOCK = (player, pos, hand, chunk) -> {
BlockState blockState = player.level.getBlockState(pos);

if (blockState.is(FTBChunksAPI.INTERACT_WHITELIST_TAG)) {
return ProtectionOverride.ALLOW;
}

if (chunk != null && chunk.teamData.canUse(player, FTBChunksTeamData.BLOCK_EDIT_AND_INTERACT_MODE)) {
return ProtectionOverride.ALLOW;
}

return ProtectionOverride.CHECK;
};

Protection EDIT_BLOCK = (player, pos, hand, chunk) -> {
BlockState blockState = player.level.getBlockState(pos);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.ftb.mods.ftbchunks.fabric;

import dev.ftb.mods.ftbchunks.data.FTBChunksTeamData;
import dev.ftb.mods.ftbchunks.data.Protection;
import dev.ftb.mods.ftbteams.event.TeamCollectPropertiesEvent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.TicketType;
import net.minecraft.world.level.ChunkPos;
Expand All @@ -16,4 +19,21 @@ public static void addChunkToForceLoaded(ServerLevel level, String modId, UUID o
level.getChunkSource().removeRegionTicket(TicketType.FORCED, chunkPos, 2, chunkPos);
}
}

public static void getTeamConfigsForPlatform(TeamCollectPropertiesEvent event) {
event.add(FTBChunksTeamData.ALLOW_FAKE_PLAYERS);
event.add(FTBChunksTeamData.BLOCK_EDIT_AND_INTERACT_MODE);
}

public static Protection getBlockPlaceProtectionForPlatform() {
return Protection.EDIT_AND_INTERACT_BLOCK;
}

public static Protection getBlockInteractProtectionForPlatform() {
return Protection.EDIT_AND_INTERACT_BLOCK;
}

public static Protection getBlockBreakProtectionForPlatform() {
return Protection.EDIT_AND_INTERACT_BLOCK;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.ftb.mods.ftbchunks.forge;

import dev.ftb.mods.ftbchunks.data.FTBChunksTeamData;
import dev.ftb.mods.ftbchunks.data.Protection;
import dev.ftb.mods.ftbteams.event.TeamCollectPropertiesEvent;
import net.minecraft.server.level.ServerLevel;
import net.minecraftforge.common.world.ForgeChunkManager;

Expand All @@ -9,4 +12,22 @@ public class FTBChunksExpectedImpl {
public static void addChunkToForceLoaded(ServerLevel level, String modId, UUID owner, int chunkX, int chunkY, boolean add) {
ForgeChunkManager.forceChunk(level, modId, owner, chunkX, chunkY, add, false);
}

public static void getTeamConfigsForPlatform(TeamCollectPropertiesEvent event) {
event.add(FTBChunksTeamData.ALLOW_FAKE_PLAYERS);
event.add(FTBChunksTeamData.BLOCK_EDIT_MODE);
event.add(FTBChunksTeamData.BLOCK_INTERACT_MODE);
}

public static Protection getBlockPlaceProtectionForPlatform() {
return Protection.EDIT_BLOCK;
}

public static Protection getBlockInteractProtectionForPlatform() {
return Protection.INTERACT_BLOCK;
}

public static Protection getBlockBreakProtectionForPlatform() {
return Protection.EDIT_BLOCK;
}
}