Skip to content

Commit

Permalink
Remove flight break speed debuff when in build mode
Browse files Browse the repository at this point in the history
Fixes #19
  • Loading branch information
Matyrobbrt committed Sep 25, 2023
1 parent 160ef7f commit 07e9df7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/lovetropics/gamemodebuild/GBConfigs.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static class Server {
final ConfigValue<String> listsConfig;

final BooleanValue enabled;
final BooleanValue removeBreakSpeedDebuff;

final Map<String, ItemFilter> filters = new HashMap<>();;

Expand All @@ -75,6 +76,7 @@ public static class Server {
});

enabled = builder.comment("Enable SurvivalPlus for all players").define("enabled", true);
removeBreakSpeedDebuff = builder.comment("If true, players will break blocks in build mode as fast as if they were not flying").define("removeBreakSpeedDebuff", true);
}

void loadLists() {
Expand Down Expand Up @@ -179,6 +181,10 @@ public void enable(boolean state) {
public boolean enabled() {
return enabled.get();
}

public boolean removeBreakSpeedDebuff() {
return removeBreakSpeedDebuff.get();
}
}

static final ForgeConfigSpec serverSpec;
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/lovetropics/gamemodebuild/GamemodeBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
import com.lovetropics.gamemodebuild.command.ItemFilterArgument;
import com.lovetropics.gamemodebuild.container.BuildContainer;
import com.lovetropics.gamemodebuild.message.GBNetwork;
import com.lovetropics.gamemodebuild.state.GBClientState;
import com.lovetropics.gamemodebuild.state.GBPlayerStore;
import com.lovetropics.gamemodebuild.state.GBServerState;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.IExtensionPoint;
Expand Down Expand Up @@ -67,4 +73,22 @@ private void setup(final FMLCommonSetupEvent event) {
public void registerCommands(RegisterCommandsEvent event) {
GamemodeBuildCommand.register(event.getDispatcher(), event.getBuildContext());
}

@SubscribeEvent
public void onBreakSpeed(final PlayerEvent.BreakSpeed event) {
if (event.getEntity().onGround()) return;

if (GBConfigs.SERVER.removeBreakSpeedDebuff() && isActive(event.getEntity())) {
// See Player#getDigSpeed, if the player is flying they break blocks 5 times slower.
// Let's revert that as it's an annoying limitation in build mode
event.setNewSpeed(event.getNewSpeed() * 5f);
}
}

private boolean isActive(Player player) {
if (player.level().isClientSide()) {
return GBClientState.isActive();
}
return player instanceof ServerPlayer sp ? GBServerState.isActiveFor(sp) : GBPlayerStore.isActive(player);
}
}

0 comments on commit 07e9df7

Please sign in to comment.