Merge remote-tracking branch 'bysco/1.20-powercap' into 1.20-alternative

This commit is contained in:
Christian Knaapen
2023-12-23 21:45:29 +01:00
34 changed files with 531 additions and 326 deletions

View File

@@ -17,6 +17,7 @@ import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import nl.requios.effortlessbuilding.buildmode.BuildModeEnum;
import nl.requios.effortlessbuilding.buildmode.ModeOptions;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.gui.buildmode.PlayerSettingsGui;
import nl.requios.effortlessbuilding.gui.buildmode.RadialMenu;
import nl.requios.effortlessbuilding.gui.buildmodifier.ModifiersScreen;
@@ -133,7 +134,7 @@ public class ClientEvents {
//Radial menu
if (keyBindings[0].isDown()) {
if (!EffortlessBuildingClient.POWER_LEVEL.isDisabled(player)) {
if (!CapabilityHandler.isDisabled(player)) {
if (!RadialMenu.instance.isVisible()) {
Minecraft.getInstance().setScreen(RadialMenu.instance);
}
@@ -174,7 +175,7 @@ public class ClientEvents {
if (player == null) return;
//Disabled if max reach is 0, might be set in the config that way.
if (EffortlessBuildingClient.POWER_LEVEL.isDisabled(player)) {
if (CapabilityHandler.isDisabled(player)) {
EffortlessBuilding.log(player, "Build modifiers are disabled until your power level has increased. Increase your power level by consuming certain items.");
} else {
mc.setScreen(new ModifiersScreen());

View File

@@ -2,10 +2,12 @@ package nl.requios.effortlessbuilding;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
@@ -14,9 +16,13 @@ import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.network.PacketDistributor;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.capability.IPowerLevel;
import nl.requios.effortlessbuilding.capability.PowerLevelCapability;
import nl.requios.effortlessbuilding.compatibility.CompatHelper;
import nl.requios.effortlessbuilding.network.ModifierSettingsPacket;
import nl.requios.effortlessbuilding.network.PacketHandler;
import nl.requios.effortlessbuilding.network.PowerLevelPacket;
import nl.requios.effortlessbuilding.systems.ServerBuildState;
import nl.requios.effortlessbuilding.utilities.PowerLevelCommand;
@@ -74,7 +80,10 @@ public class CommonEvents {
//Don't cancel event if our custom logic is breaking blocks
if (EffortlessBuilding.SERVER_BLOCK_PLACER.isPlacingOrBreakingBlocks()) return;
if (!ServerBuildState.isLikeVanilla(player) && EffortlessBuilding.SERVER_POWER_LEVEL.canBreakFar(player)) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel == null) return; //Should never be null but just to be sure
if (!ServerBuildState.isLikeVanilla(player) && powerLevel.canBreakFar(player)) {
event.setCanceled(true);
}
}
@@ -94,7 +103,34 @@ public class CommonEvents {
ServerBuildState.handleNewPlayer(player);
PacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new ModifierSettingsPacket(player));
EffortlessBuilding.SERVER_POWER_LEVEL.sendToClient(player);
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel == null) return; //Should never be null but just to be sure
PacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new PowerLevelPacket(powerLevel.getPowerLevel()));
}
@SubscribeEvent
public static void registerCaps(AttachCapabilitiesEvent<Entity> event) {
if (event.getObject() instanceof Player) {
event.addCapability(CapabilityHandler.POWER_LEVEL_CAP, new PowerLevelCapability());
}
}
@SubscribeEvent
public static void onClone(PlayerEvent.Clone event) {
// If not dead, player is returning from the End
if (!event.isWasDeath()) return;
Player original = event.getOriginal();
Player clone = event.getEntity();
// Copy the power level from the original player to the clone
original.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).ifPresent(dataOriginal ->
clone.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).ifPresent(dataClone -> {
dataClone.setPowerLevel(dataOriginal.getPowerLevel());
})
);
}
@SubscribeEvent

View File

@@ -35,7 +35,6 @@ import nl.requios.effortlessbuilding.proxy.IProxy;
import nl.requios.effortlessbuilding.proxy.ServerProxy;
import nl.requios.effortlessbuilding.systems.ItemUsageTracker;
import nl.requios.effortlessbuilding.systems.ServerBlockPlacer;
import nl.requios.effortlessbuilding.systems.ServerPowerLevel;
import nl.requios.effortlessbuilding.systems.UndoRedo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -54,7 +53,6 @@ public class EffortlessBuilding {
public static final ServerBlockPlacer SERVER_BLOCK_PLACER = new ServerBlockPlacer();
public static final UndoRedo UNDO_REDO = new UndoRedo();
public static final ItemUsageTracker ITEM_USAGE_TRACKER = new ItemUsageTracker();
public static final ServerPowerLevel SERVER_POWER_LEVEL = new ServerPowerLevel();
//Registration
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);

View File

@@ -20,7 +20,6 @@ public class EffortlessBuildingClient {
public static final BlockPreviews BLOCK_PREVIEWS = new BlockPreviews();
public static final BuilderFilter BUILDER_FILTER = new BuilderFilter();
public static final ItemUsageTracker ITEM_USAGE_TRACKER = new ItemUsageTracker();
public static final PowerLevel POWER_LEVEL = new PowerLevel();
public static void onConstructorClient(IEventBus modEventBus, IEventBus forgeEventBus) {
modEventBus.addListener(EffortlessBuildingClient::clientSetup);

View File

@@ -5,6 +5,7 @@ import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.utilities.BlockEntry;
import nl.requios.effortlessbuilding.utilities.BlockSet;
@@ -64,7 +65,7 @@ public abstract class ThreeClicksBuildMode extends BaseBuildMode {
if (secondPos == null) return;
//Limit amount of blocks we can place per row
int axisLimit = EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPerAxis(player);
int axisLimit = CapabilityHandler.getMaxBlocksPerAxis(player, false);
int x1 = firstPos.getX(), x2 = secondPos.getX();
int y1 = firstPos.getY(), y2 = secondPos.getY();
@@ -93,7 +94,7 @@ public abstract class ThreeClicksBuildMode extends BaseBuildMode {
if (thirdPos == null) return;
//Limit amount of blocks you can place per row
int axisLimit = EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPerAxis(player);
int axisLimit = CapabilityHandler.getMaxBlocksPerAxis(player, false);
int x1 = firstPos.getX(), x2 = secondPos.getX(), x3 = thirdPos.getX();
int y1 = firstPos.getY(), y2 = secondPos.getY(), y3 = thirdPos.getY();
@@ -139,7 +140,7 @@ public abstract class ThreeClicksBuildMode extends BaseBuildMode {
criteriaList.add(new HeightCriteria(zBound, secondPos, start));
//Remove invalid criteria
int reach = EffortlessBuildingClient.POWER_LEVEL.getBuildModeReach(player);
int reach = CapabilityHandler.getBuildModeReach(player);
criteriaList.removeIf(criteria -> !criteria.isValid(start, look, reach, player, skipRaytrace));
//If none are valid, return empty list of blocks

View File

@@ -4,6 +4,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.utilities.BlockEntry;
import nl.requios.effortlessbuilding.utilities.BlockSet;
@@ -48,7 +49,7 @@ public abstract class TwoClicksBuildMode extends BaseBuildMode {
if (secondPos == null) return;
//Limit amount of blocks we can place per row
int axisLimit = EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPerAxis(player);
int axisLimit = CapabilityHandler.getMaxBlocksPerAxis(player, false);
int x1 = firstPos.getX(), x2 = secondPos.getX();
int y1 = firstPos.getY(), y2 = secondPos.getY();

View File

@@ -3,10 +3,10 @@ package nl.requios.effortlessbuilding.buildmode.buildmodes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.Vec3;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmode.BuildModes;
import nl.requios.effortlessbuilding.buildmode.ModeOptions;
import nl.requios.effortlessbuilding.buildmode.TwoClicksBuildMode;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import java.util.ArrayList;
import java.util.List;
@@ -24,7 +24,7 @@ public class Floor extends TwoClicksBuildMode {
criteriaList.add(new Criteria(yBound, start));
//Remove invalid criteria
int reach = EffortlessBuildingClient.POWER_LEVEL.getBuildModeReach(player);
int reach = CapabilityHandler.getBuildModeReach(player);
criteriaList.removeIf(criteria -> !criteria.isValid(start, look, reach, player, skipRaytrace));
//If none are valid, return empty list of blocks

View File

@@ -3,9 +3,9 @@ package nl.requios.effortlessbuilding.buildmode.buildmodes;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmode.BuildModes;
import nl.requios.effortlessbuilding.buildmode.TwoClicksBuildMode;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import java.util.ArrayList;
import java.util.List;
@@ -31,7 +31,7 @@ public class Line extends TwoClicksBuildMode {
criteriaList.add(new Criteria(zBound, firstPos, start));
//Remove invalid criteria
int reach = EffortlessBuildingClient.POWER_LEVEL.getBuildModeReach(player);
int reach = CapabilityHandler.getBuildModeReach(player);
criteriaList.removeIf(criteria -> !criteria.isValid(start, look, reach, player, skipRaytrace));
//If none are valid, return empty list of blocks

View File

@@ -2,9 +2,9 @@ package nl.requios.effortlessbuilding.buildmode.buildmodes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmode.ModeOptions;
import nl.requios.effortlessbuilding.buildmode.ThreeClicksBuildMode;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import java.util.ArrayList;
import java.util.List;
@@ -15,7 +15,7 @@ public class SlopeFloor extends ThreeClicksBuildMode {
public static List<BlockPos> getSlopeFloorBlocks(Player player, int x1, int y1, int z1, int x2, int y2, int z2, int x3, int y3, int z3) {
List<BlockPos> list = new ArrayList<>();
int axisLimit = EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPerAxis(player);
int axisLimit = CapabilityHandler.getMaxBlocksPerAxis(player, false);
//Determine whether to use x or z axis to slope up
boolean onXAxis = true;

View File

@@ -3,10 +3,10 @@ package nl.requios.effortlessbuilding.buildmode.buildmodes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.Vec3;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmode.BuildModes;
import nl.requios.effortlessbuilding.buildmode.ModeOptions;
import nl.requios.effortlessbuilding.buildmode.TwoClicksBuildMode;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import java.util.ArrayList;
import java.util.List;
@@ -28,7 +28,7 @@ public class Wall extends TwoClicksBuildMode {
criteriaList.add(new Criteria(zBound, firstPos, start, look));
//Remove invalid criteria
int reach = EffortlessBuildingClient.POWER_LEVEL.getBuildModeReach(player);
int reach = CapabilityHandler.getBuildModeReach(player);
criteriaList.removeIf(criteria -> !criteria.isValid(start, look, reach, player, skipRaytrace));
//If none are valid, return empty list of blocks

View File

@@ -6,7 +6,7 @@ import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.Vec3;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.utilities.BlockEntry;
import nl.requios.effortlessbuilding.utilities.BlockSet;
@@ -42,7 +42,7 @@ public class Mirror extends BaseModifier {
@Override
public void onPowerLevelChanged(int powerLevel) {
radius = EffortlessBuildingClient.POWER_LEVEL.getMaxMirrorRadius(Minecraft.getInstance().player);
radius = CapabilityHandler.getMaxMirrorRadius(Minecraft.getInstance().player, false);
}
private void performMirrorX(BlockSet blocks, BlockEntry blockEntry) {

View File

@@ -9,7 +9,7 @@ import net.minecraft.world.level.block.Rotation;
import net.minecraft.core.BlockPos;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec3;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.utilities.BlockEntry;
import nl.requios.effortlessbuilding.utilities.BlockSet;
@@ -43,7 +43,7 @@ public class RadialMirror extends BaseModifier {
@Override
public void onPowerLevelChanged(int powerLevel) {
radius = EffortlessBuildingClient.POWER_LEVEL.getMaxMirrorRadius(Minecraft.getInstance().player);
radius = CapabilityHandler.getMaxMirrorRadius(Minecraft.getInstance().player, false);
}
public void performRadialMirror(BlockSet blocks, BlockEntry blockEntry) {

View File

@@ -0,0 +1,137 @@
package nl.requios.effortlessbuilding.capability;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.network.PacketDistributor;
import nl.requios.effortlessbuilding.CommonConfig;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.network.PacketHandler;
import nl.requios.effortlessbuilding.network.PowerLevelPacket;
public class CapabilityHandler {
public static final ResourceLocation POWER_LEVEL_CAP = new ResourceLocation(EffortlessBuilding.MODID, "power_level");
public static final Capability<IPowerLevel> POWER_LEVEL_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {
});
public static void syncToClient(Player player) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel == null) return; //Should never be null but just to be sure
PacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new PowerLevelPacket(powerLevel.getPowerLevel()));
}
//Helper methods to reduce boilerplate code
public static boolean canReplaceBlocks(Player player) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.canReplaceBlocks(player);
}
}
return false;
}
public static int getMaxBlocksPerAxis(Player player, boolean nextPowerLevel) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.getMaxBlocksPerAxis(player, nextPowerLevel);
}
}
return CommonConfig.maxBlocksPerAxis.level0.get();
}
public static int getMaxBlocksPlacedAtOnce(Player player, boolean nextPowerLevel) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.getMaxBlocksPlacedAtOnce(player, nextPowerLevel);
}
}
return CommonConfig.maxBlocksPlacedAtOnce.level0.get();
}
public static int getMaxMirrorRadius(Player player, boolean nextPowerLevel) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.getMaxMirrorRadius(player, nextPowerLevel);
}
}
return CommonConfig.maxMirrorRadius.level0.get();
}
public static int getBuildModeReach(Player player) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.getBuildModeReach(player);
}
}
return CommonConfig.maxMirrorRadius.level0.get() + 6;
}
public static int getPlacementReach(Player player, boolean nextPowerLevel) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.getPlacementReach(player, nextPowerLevel);
}
}
return CommonConfig.reach.level0.get();
}
public static int getPowerLevel(Player player) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.getPowerLevel();
}
}
return 0;
}
public static int getNextPowerLevel(Player player) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.getNextPowerLevel();
}
}
return 0;
}
public static boolean canIncreasePowerLevel(Player player) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.canIncreasePowerLevel();
}
}
return false;
}
public static boolean isDisabled(Player player) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.isDisabled(player);
}
}
return false;
}
public static boolean canBreakFar(Player player) {
if (player != null) {
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
return powerLevel.canBreakFar(player);
}
}
return false;
}
}

View File

@@ -0,0 +1,38 @@
package nl.requios.effortlessbuilding.capability;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.capabilities.AutoRegisterCapability;
@AutoRegisterCapability
public interface IPowerLevel {
int getPowerLevel();
int getNextPowerLevel();
void setPowerLevel(int powerLevel);
boolean canIncreasePowerLevel();
void increasePowerLevel();
int getPlacementReach(Player player, boolean nextPowerLevel);
int getBuildModeReach(Player player);
int getMaxBlocksPlacedAtOnce(Player player, boolean nextPowerLevel);
int getMaxBlocksPerAxis(Player player, boolean nextPowerLevel);
int getMaxMirrorRadius(Player player, boolean nextPowerLevel);
boolean isDisabled(Player player);
boolean canBreakFar(Player player);
boolean canReplaceBlocks(Player player);
}

View File

@@ -0,0 +1,127 @@
package nl.requios.effortlessbuilding.capability;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import nl.requios.effortlessbuilding.CommonConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PowerLevelCapability implements IPowerLevel, ICapabilitySerializable<CompoundTag> {
public static final int MAX_POWER_LEVEL = 3; //Common access
private int powerLevel = 0;
@Override
public int getPowerLevel() {
return this.powerLevel;
}
@Override
public int getNextPowerLevel() {
return Math.min(getPowerLevel() + 1, MAX_POWER_LEVEL);
}
@Override
public void setPowerLevel(int powerLevel) {
this.powerLevel = powerLevel;
}
@Override
public boolean canIncreasePowerLevel() {
return getPowerLevel() < MAX_POWER_LEVEL;
}
@Override
public void increasePowerLevel() {
if (canIncreasePowerLevel()) {
setPowerLevel(getPowerLevel() + 1);
}
}
@Override
public int getPlacementReach(Player player, boolean nextPowerLevel) {
if (player.isCreative()) return CommonConfig.reach.creative.get();
return switch (nextPowerLevel ? getNextPowerLevel() : getPowerLevel()) {
case 1 -> CommonConfig.reach.level1.get();
case 2 -> CommonConfig.reach.level2.get();
case 3 -> CommonConfig.reach.level3.get();
default -> CommonConfig.reach.level0.get();
};
}
//How far away we can detect the second and third click of build modes (distance to player)
@Override
public int getBuildModeReach(Player player) {
//A bit further than placement reach, so you can build lines when looking to the side without having to move.
return getPlacementReach(player, false) + 6;
}
@Override
public int getMaxBlocksPlacedAtOnce(Player player, boolean nextPowerLevel) {
if (player.isCreative()) return CommonConfig.maxBlocksPlacedAtOnce.creative.get();
return switch (nextPowerLevel ? getNextPowerLevel() : getPowerLevel()) {
case 1 -> CommonConfig.maxBlocksPlacedAtOnce.level1.get();
case 2 -> CommonConfig.maxBlocksPlacedAtOnce.level2.get();
case 3 -> CommonConfig.maxBlocksPlacedAtOnce.level3.get();
default -> CommonConfig.maxBlocksPlacedAtOnce.level0.get();
};
}
@Override
public int getMaxBlocksPerAxis(Player player, boolean nextPowerLevel) {
if (player.isCreative()) return CommonConfig.maxBlocksPerAxis.creative.get();
return switch (nextPowerLevel ? getNextPowerLevel() : getPowerLevel()) {
case 1 -> CommonConfig.maxBlocksPerAxis.level1.get();
case 2 -> CommonConfig.maxBlocksPerAxis.level2.get();
case 3 -> CommonConfig.maxBlocksPerAxis.level3.get();
default -> CommonConfig.maxBlocksPerAxis.level0.get();
};
}
@Override
public int getMaxMirrorRadius(Player player, boolean nextPowerLevel) {
if (player.isCreative()) return CommonConfig.maxMirrorRadius.creative.get();
return switch (getPowerLevel() + (nextPowerLevel ? 1 : 0)) {
case 1 -> CommonConfig.maxMirrorRadius.level1.get();
case 2 -> CommonConfig.maxMirrorRadius.level2.get();
case 3 -> CommonConfig.maxMirrorRadius.level3.get();
default -> CommonConfig.maxMirrorRadius.level0.get();
};
}
@Override
public boolean isDisabled(Player player) {
return getMaxBlocksPlacedAtOnce(player, false) <= 0 || getMaxBlocksPerAxis(player, false) <= 0;
}
@Override
public boolean canBreakFar(Player player) {
return player.getAbilities().instabuild;
}
@Override
public boolean canReplaceBlocks(Player player) {
return player.getAbilities().instabuild;
}
@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
return CapabilityHandler.POWER_LEVEL_CAPABILITY.orEmpty(cap, LazyOptional.of(() -> this));
}
@Override
public CompoundTag serializeNBT() {
CompoundTag tag = new CompoundTag();
tag.putInt("powerLevel", getPowerLevel());
return tag;
}
@Override
public void deserializeNBT(CompoundTag nbt) {
setPowerLevel(nbt.getInt("powerLevel"));
}
}

View File

@@ -15,7 +15,6 @@ import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import nl.requios.effortlessbuilding.create.foundation.gui.AllGuiTextures;
import nl.requios.effortlessbuilding.create.foundation.gui.TickableGuiEventListener;
import nl.requios.effortlessbuilding.create.foundation.gui.widget.AbstractSimiWidget;

View File

@@ -26,6 +26,7 @@ import nl.requios.effortlessbuilding.buildmode.BuildModeEnum;
import nl.requios.effortlessbuilding.buildmode.ModeOptions;
import nl.requios.effortlessbuilding.buildmode.ModeOptions.ActionEnum;
import nl.requios.effortlessbuilding.buildmode.ModeOptions.OptionEnum;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.create.foundation.item.ItemDescription;
import nl.requios.effortlessbuilding.create.foundation.item.TooltipHelper;
import nl.requios.effortlessbuilding.create.foundation.utility.Color;
@@ -151,8 +152,10 @@ public class RadialMenu extends Screen {
modes.add(new MenuRegion(mode));
}
//Add actions
boolean canReplace = minecraft.player != null && EffortlessBuildingClient.POWER_LEVEL.canReplaceBlocks(minecraft.player);
boolean canReplace = CapabilityHandler.canReplaceBlocks(minecraft.player);
// buttons.add(new MenuButton(ActionEnum.OPEN_PLAYER_SETTINGS, -buttonDistance - 65, -13, Direction.UP));
if (canReplace) {
@@ -334,7 +337,7 @@ public class RadialMenu extends Screen {
guiGraphics.drawString(font, credits, width - font.width(credits) - 4, height - 10, watermarkTextColor);
//Draw power level info
String powerLevelValue = minecraft.player.isCreative() ? "Creative" : String.valueOf(EffortlessBuildingClient.POWER_LEVEL.getPowerLevel());
String powerLevelValue = minecraft.player.isCreative() ? "Creative" : String.valueOf(CapabilityHandler.getPowerLevel(minecraft.player));
String powerLevelText = I18n.get("key.effortlessbuilding.power_level") + ": " + powerLevelValue;
guiGraphics.drawString(font, powerLevelText, width - font.width(powerLevelText) - 4, height - 22, minecraft.player.isCreative() ? watermarkTextColor : ChatFormatting.DARK_PURPLE.getColor());
@@ -342,19 +345,19 @@ public class RadialMenu extends Screen {
if (mouseX >= width - font.width(powerLevelText) - 14 && mouseX <= width && mouseY >= height - 24 && mouseY <= height) {
var tooltip = new ArrayList<Component>();
tooltip.add(Components.literal(powerLevelText).withStyle(ChatFormatting.DARK_PURPLE));
int placementReach = EffortlessBuildingClient.POWER_LEVEL.getPlacementReach(minecraft.player);
int placementReach = CapabilityHandler.getPlacementReach(minecraft.player, false);
tooltip.add(Components.translatable("key.effortlessbuilding.placement_reach").withStyle(ChatFormatting.GRAY).append(": " + (placementReach == 0 ? "vanilla" : placementReach + " blocks")));
tooltip.add(Components.translatable("key.effortlessbuilding.max_blocks_per_axis").withStyle(ChatFormatting.GRAY).append(": " + EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPerAxis(minecraft.player)));
tooltip.add(Components.translatable("key.effortlessbuilding.max_blocks_placed_at_once").withStyle(ChatFormatting.GRAY).append(": " + EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPlacedAtOnce(minecraft.player)));
tooltip.add(Components.translatable("key.effortlessbuilding.max_mirror_radius").withStyle(ChatFormatting.GRAY).append(": " + EffortlessBuildingClient.POWER_LEVEL.getMaxMirrorRadius(minecraft.player) + " blocks"));
tooltip.add(Components.translatable("key.effortlessbuilding.max_blocks_per_axis").withStyle(ChatFormatting.GRAY).append(": " + CapabilityHandler.getMaxBlocksPerAxis(minecraft.player, false)));
tooltip.add(Components.translatable("key.effortlessbuilding.max_blocks_placed_at_once").withStyle(ChatFormatting.GRAY).append(": " + CapabilityHandler.getMaxBlocksPlacedAtOnce(minecraft.player, false)));
tooltip.add(Components.translatable("key.effortlessbuilding.max_mirror_radius").withStyle(ChatFormatting.GRAY).append(": " + CapabilityHandler.getMaxMirrorRadius(minecraft.player, false) + " blocks"));
if (EffortlessBuildingClient.POWER_LEVEL.canIncreasePowerLevel() && !minecraft.player.isCreative()) {
if (CapabilityHandler.canIncreasePowerLevel(minecraft.player) && !minecraft.player.isCreative()) {
tooltip.add(Components.literal(""));
tooltip.add(Components.translatable("key.effortlessbuilding.next_power_level").withStyle(ChatFormatting.DARK_AQUA).append(": " + EffortlessBuildingClient.POWER_LEVEL.getNextPowerLevel()));
tooltip.add(Components.translatable("key.effortlessbuilding.placement_reach").withStyle(ChatFormatting.GRAY).append(": " + EffortlessBuildingClient.POWER_LEVEL.getPlacementReach(minecraft.player, true) + " blocks"));
tooltip.add(Components.translatable("key.effortlessbuilding.max_blocks_per_axis").withStyle(ChatFormatting.GRAY).append(": " + EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPerAxis(minecraft.player, true)));
tooltip.add(Components.translatable("key.effortlessbuilding.max_blocks_placed_at_once").withStyle(ChatFormatting.GRAY).append(": " + EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPlacedAtOnce(minecraft.player, true)));
tooltip.add(Components.translatable("key.effortlessbuilding.max_mirror_radius").withStyle(ChatFormatting.GRAY).append(": " + EffortlessBuildingClient.POWER_LEVEL.getMaxMirrorRadius(minecraft.player, true) + " blocks"));
tooltip.add(Components.translatable("key.effortlessbuilding.next_power_level").withStyle(ChatFormatting.DARK_AQUA).append(": " + CapabilityHandler.getNextPowerLevel(minecraft.player)));
tooltip.add(Components.translatable("key.effortlessbuilding.placement_reach").withStyle(ChatFormatting.GRAY).append(": " + CapabilityHandler.getPlacementReach(minecraft.player, true) + " blocks"));
tooltip.add(Components.translatable("key.effortlessbuilding.max_blocks_per_axis").withStyle(ChatFormatting.GRAY).append(": " + CapabilityHandler.getMaxBlocksPerAxis(minecraft.player, true)));
tooltip.add(Components.translatable("key.effortlessbuilding.max_blocks_placed_at_once").withStyle(ChatFormatting.GRAY).append(": " + CapabilityHandler.getMaxBlocksPlacedAtOnce(minecraft.player, true)));
tooltip.add(Components.translatable("key.effortlessbuilding.max_mirror_radius").withStyle(ChatFormatting.GRAY).append(": " + CapabilityHandler.getMaxMirrorRadius(minecraft.player, true) + " blocks"));
tooltip.add(Components.literal(""));
tooltip.addAll(TooltipHelper.cutTextComponent(Components.translatable("key.effortlessbuilding.next_power_level_how"), ChatFormatting.GRAY, ChatFormatting.WHITE));
}

View File

@@ -7,9 +7,9 @@ import net.minecraft.network.chat.Component;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import nl.requios.effortlessbuilding.AllGuiTextures;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmodifier.Array;
import nl.requios.effortlessbuilding.buildmodifier.BaseModifier;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.create.foundation.gui.widget.ScrollInput;
import nl.requios.effortlessbuilding.gui.elements.LabeledScrollInput;
import nl.requios.effortlessbuilding.utilities.MathHelper;
@@ -85,7 +85,7 @@ public class ArrayEntry extends BaseModifierEntry<Array> {
super.onValueChanged();
int currentReach = Math.max(-1, getArrayReach());
int maxReach = EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPerAxis(Minecraft.getInstance().player);
int maxReach = CapabilityHandler.getMaxBlocksPerAxis(Minecraft.getInstance().player, false);
ChatFormatting reachColor = isCurrentReachValid(currentReach, maxReach) ? ChatFormatting.GRAY : ChatFormatting.RED;
var reachText = "" + reachColor + currentReach + ChatFormatting.GRAY + "/" + ChatFormatting.GRAY + maxReach;
reachLabel.text = Component.literal(reachText);

View File

@@ -8,9 +8,9 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import nl.requios.effortlessbuilding.AllGuiTextures;
import nl.requios.effortlessbuilding.AllIcons;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmodifier.BaseModifier;
import nl.requios.effortlessbuilding.buildmodifier.Mirror;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.create.foundation.gui.widget.IconButton;
import nl.requios.effortlessbuilding.create.foundation.gui.widget.ScrollInput;
import nl.requios.effortlessbuilding.create.foundation.utility.Components;
@@ -101,7 +101,7 @@ public class MirrorEntry extends BaseModifierEntry<Mirror> {
//Radius
radiusInput = new LabeledScrollInput(0, 0, 27, 18)
.withRange(0, EffortlessBuildingClient.POWER_LEVEL.getMaxMirrorRadius(Minecraft.getInstance().player))
.withRange(0, CapabilityHandler.getMaxMirrorRadius(Minecraft.getInstance().player, false))
.titled(Minecraft.getInstance().player.isCreative() ?
Component.literal("Radius") :
Component.literal("Radius. Use Reach Upgrade items to increase maximum."))

View File

@@ -8,9 +8,9 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import nl.requios.effortlessbuilding.AllGuiTextures;
import nl.requios.effortlessbuilding.AllIcons;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmodifier.BaseModifier;
import nl.requios.effortlessbuilding.buildmodifier.RadialMirror;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.create.foundation.gui.widget.IconButton;
import nl.requios.effortlessbuilding.create.foundation.gui.widget.ScrollInput;
import nl.requios.effortlessbuilding.create.foundation.utility.Components;
@@ -107,7 +107,7 @@ public class RadialMirrorEntry extends BaseModifierEntry<RadialMirror> {
//Radius
radiusInput = new LabeledScrollInput(0, 0, 27, 18)
.withRange(0, EffortlessBuildingClient.POWER_LEVEL.getMaxMirrorRadius(Minecraft.getInstance().player))
.withRange(0, CapabilityHandler.getMaxMirrorRadius(Minecraft.getInstance().player, false))
.titled(Minecraft.getInstance().player.isCreative() ?
Component.literal("Radius") :
Component.literal("Radius. Use Reach Upgrade items to increase maximum."))

View File

@@ -3,8 +3,8 @@ package nl.requios.effortlessbuilding.item;
import net.minecraft.ChatFormatting;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
@@ -13,7 +13,8 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.capability.IPowerLevel;
import nl.requios.effortlessbuilding.create.foundation.item.TooltipHelper;
import javax.annotation.Nullable;
@@ -29,33 +30,34 @@ public class PowerLevelItem extends Item {
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
if (powerLevel.canIncreasePowerLevel()) {
if (!world.isClientSide) {
powerLevel.increasePowerLevel();
EffortlessBuilding.log(player, "Upgraded power level to " + powerLevel.getPowerLevel());
if (world.isClientSide){
stack.shrink(1);
if (EffortlessBuildingClient.POWER_LEVEL.canIncreasePowerLevel()) {
world.playSound((Player) null, player.blockPosition(), SoundEvents.PLAYER_LEVELUP, SoundSource.PLAYERS, 1f, 1f);
EffortlessBuildingClient.POWER_LEVEL.increasePowerLevel();
EffortlessBuilding.log(player, "Upgraded power level to " + EffortlessBuildingClient.POWER_LEVEL.getPowerLevel());
player.setItemInHand(hand, ItemStack.EMPTY);
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("entity.player.levelup"));
player.playSound(soundEvent, 1f, 1f);
return InteractionResultHolder.consume(player.getItemInHand(hand));
CapabilityHandler.syncToClient(player);
}
return InteractionResultHolder.sidedSuccess(stack, world.isClientSide());
} else {
if (!world.isClientSide) {
EffortlessBuilding.log(player, "Already reached maximum power level!");
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("item.armor.equip_leather"));
player.playSound(soundEvent, 1f, 1f);
world.playSound((Player) null, player.blockPosition(), SoundEvents.ARMOR_EQUIP_LEATHER, SoundSource.PLAYERS, 1f, 1f);
}
return InteractionResultHolder.fail(player.getItemInHand(hand));
}
} else {
return InteractionResultHolder.consume(player.getItemInHand(hand));
}
return super.use(world, player, hand);
}
@Override

View File

@@ -3,8 +3,8 @@ package nl.requios.effortlessbuilding.item;
import net.minecraft.ChatFormatting;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
@@ -14,7 +14,8 @@ import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import nl.requios.effortlessbuilding.CommonConfig;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.capability.IPowerLevel;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -30,28 +31,29 @@ public class ReachUpgrade1Item extends Item {
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
if (!world.isClientSide) return InteractionResultHolder.consume(player.getItemInHand(hand));
int currentLevel = EffortlessBuildingClient.POWER_LEVEL.getPowerLevel();
ItemStack stack = player.getItemInHand(hand);
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
int currentLevel = powerLevel.getPowerLevel();
if (currentLevel == 0) {
if (!world.isClientSide) {
powerLevel.increasePowerLevel();
EffortlessBuilding.log(player, "Upgraded power level to " + powerLevel.getPowerLevel());
EffortlessBuildingClient.POWER_LEVEL.increasePowerLevel();
EffortlessBuilding.log(player, "Upgraded power level to " + EffortlessBuildingClient.POWER_LEVEL.getPowerLevel());
player.setItemInHand(hand, ItemStack.EMPTY);
stack.shrink(1);
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("entity.player.levelup"));
player.playSound(soundEvent, 1f, 1f);
return InteractionResultHolder.consume(player.getItemInHand(hand));
world.playSound((Player) null, player.blockPosition(), SoundEvents.PLAYER_LEVELUP, SoundSource.PLAYERS, 1f, 1f);
CapabilityHandler.syncToClient(player);
}
return InteractionResultHolder.sidedSuccess(stack, world.isClientSide());
} else if (currentLevel > 0) {
if (!world.isClientSide && hand == InteractionHand.MAIN_HAND) {
EffortlessBuilding.log(player, "Already used this upgrade! Current power level is " + powerLevel.getPowerLevel() + ".");
EffortlessBuilding.log(player, "Already used this upgrade! Current power level is " + EffortlessBuildingClient.POWER_LEVEL.getPowerLevel() + ".");
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("item.armor.equip_leather"));
player.playSound(soundEvent, 1f, 1f);
world.playSound((Player) null, player.blockPosition(), SoundEvents.ARMOR_EQUIP_LEATHER, SoundSource.PLAYERS, 1f, 1f);
}
}
}
return InteractionResultHolder.fail(player.getItemInHand(hand));

View File

@@ -3,8 +3,8 @@ package nl.requios.effortlessbuilding.item;
import net.minecraft.ChatFormatting;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
@@ -14,7 +14,8 @@ import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import nl.requios.effortlessbuilding.CommonConfig;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.capability.IPowerLevel;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -28,38 +29,38 @@ public class ReachUpgrade2Item extends Item {
super(new Item.Properties().stacksTo(1));
}
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
if (!world.isClientSide) return InteractionResultHolder.consume(player.getItemInHand(hand));
int currentLevel = EffortlessBuildingClient.POWER_LEVEL.getPowerLevel();
ItemStack stack = player.getItemInHand(hand);
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
int currentLevel = powerLevel.getPowerLevel();
if (currentLevel == 1) {
if (!world.isClientSide) {
powerLevel.increasePowerLevel();
EffortlessBuilding.log(player, "Upgraded power level to " + powerLevel.getPowerLevel());
EffortlessBuildingClient.POWER_LEVEL.increasePowerLevel();
EffortlessBuilding.log(player, "Upgraded power level to " + EffortlessBuildingClient.POWER_LEVEL.getPowerLevel());
player.setItemInHand(hand, ItemStack.EMPTY);
stack.shrink(1);
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("entity.player.levelup"));
player.playSound(soundEvent, 1f, 1f);
return InteractionResultHolder.consume(player.getItemInHand(hand));
world.playSound((Player) null, player.blockPosition(), SoundEvents.PLAYER_LEVELUP, SoundSource.PLAYERS, 1f, 1f);
CapabilityHandler.syncToClient(player);
}
return InteractionResultHolder.sidedSuccess(stack, world.isClientSide());
} else if (currentLevel < 1) {
if (!world.isClientSide) {
EffortlessBuilding.log(player, "Use Reach Upgrade 1 first.");
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("item.armor.equip_leather"));
player.playSound(soundEvent, 1f, 1f);
world.playSound((Player) null, player.blockPosition(), SoundEvents.ARMOR_EQUIP_LEATHER, SoundSource.PLAYERS, 1f, 1f);
}
} else if (currentLevel > 1) {
if (!world.isClientSide) {
EffortlessBuilding.log(player, "Already used this upgrade! Current power level is " + powerLevel.getPowerLevel() + ".");
EffortlessBuilding.log(player, "Already used this upgrade! Current power level is " + EffortlessBuildingClient.POWER_LEVEL.getPowerLevel() + ".");
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("item.armor.equip_leather"));
player.playSound(soundEvent, 1f, 1f);
world.playSound((Player) null, player.blockPosition(), SoundEvents.ARMOR_EQUIP_LEATHER, SoundSource.PLAYERS, 1f, 1f);
}
}
}
return InteractionResultHolder.fail(player.getItemInHand(hand));

View File

@@ -3,8 +3,8 @@ package nl.requios.effortlessbuilding.item;
import net.minecraft.ChatFormatting;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
@@ -14,7 +14,8 @@ import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import nl.requios.effortlessbuilding.CommonConfig;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.capability.IPowerLevel;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -30,37 +31,36 @@ public class ReachUpgrade3Item extends Item {
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
if (!world.isClientSide) return InteractionResultHolder.consume(player.getItemInHand(hand));
int currentLevel = EffortlessBuildingClient.POWER_LEVEL.getPowerLevel();
ItemStack stack = player.getItemInHand(hand);
IPowerLevel powerLevel = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerLevel != null) {
int currentLevel = powerLevel.getPowerLevel();
if (currentLevel == 2) {
if (!world.isClientSide) {
powerLevel.increasePowerLevel();
EffortlessBuilding.log(player, "Upgraded power level to " + powerLevel.getPowerLevel());
EffortlessBuildingClient.POWER_LEVEL.increasePowerLevel();
EffortlessBuilding.log(player, "Upgraded power level to " + EffortlessBuildingClient.POWER_LEVEL.getPowerLevel());
player.setItemInHand(hand, ItemStack.EMPTY);
stack.shrink(1);
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("entity.player.levelup"));
player.playSound(soundEvent, 1f, 1f);
return InteractionResultHolder.consume(player.getItemInHand(hand));
world.playSound((Player) null, player.blockPosition(), SoundEvents.PLAYER_LEVELUP, SoundSource.PLAYERS, 1f, 1f);
CapabilityHandler.syncToClient(player);
}
return InteractionResultHolder.sidedSuccess(stack, world.isClientSide());
} else if (currentLevel < 2) {
if (!world.isClientSide) {
if (currentLevel == 0) EffortlessBuilding.log(player, "Use Reach Upgrade 1 and 2 first.");
if (currentLevel == 1) EffortlessBuilding.log(player, "Use Reach Upgrade 2 first.");
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("item.armor.equip_leather"));
player.playSound(soundEvent, 1f, 1f);
world.playSound((Player) null, player.blockPosition(), SoundEvents.ARMOR_EQUIP_LEATHER, SoundSource.PLAYERS, 1f, 1f);
}
} else if (currentLevel > 2) {
if (!world.isClientSide) {
EffortlessBuilding.log(player, "Already used this upgrade! Current power level is " + powerLevel.getPowerLevel() + ".");
EffortlessBuilding.log(player, "Already used this upgrade! Current power level is " + EffortlessBuildingClient.POWER_LEVEL.getPowerLevel() + ".");
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation("item.armor.equip_leather"));
player.playSound(soundEvent, 1f, 1f);
world.playSound((Player) null, player.blockPosition(), SoundEvents.ARMOR_EQUIP_LEATHER, SoundSource.PLAYERS, 1f, 1f);
}
}
}
return InteractionResultHolder.fail(player.getItemInHand(hand));

View File

@@ -17,9 +17,9 @@ public class PacketHandler {
PROTOCOL_VERSION::equals
);
public static void register() {
int id = 0;
private static int id = 0;
public static void register() {
INSTANCE.registerMessage(id++, IsUsingBuildModePacket.class, IsUsingBuildModePacket::encode, IsUsingBuildModePacket::decode,
IsUsingBuildModePacket.Handler::handle, Optional.of(NetworkDirection.PLAY_TO_SERVER));
INSTANCE.registerMessage(id++, IsQuickReplacingPacket.class, IsQuickReplacingPacket::encode, IsQuickReplacingPacket::decode,

View File

@@ -1,14 +1,15 @@
package nl.requios.effortlessbuilding.network;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.network.NetworkEvent;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import java.util.function.Supplier;
/**
* Sync power level between server and client, for saving and loading.
* Sync power level from server to client
*/
public class PowerLevelPacket {
@@ -31,19 +32,19 @@ public class PowerLevelPacket {
public static class Handler {
public static void handle(PowerLevelPacket message, Supplier<NetworkEvent.Context> ctx) {
if (ctx.get().getDirection().getReceptionSide().isServer()) {
ctx.get().enqueueWork(() -> {
var player = ctx.get().getSender();
//To server, save to persistent player data
EffortlessBuilding.SERVER_POWER_LEVEL.setPowerLevel(player, message.powerLevel);
});
} else {
ctx.get().enqueueWork(() -> {
//To client, load into system
EffortlessBuildingClient.POWER_LEVEL.setPowerLevel(message.powerLevel);
NetworkEvent.Context context = ctx.get();
context.enqueueWork(() -> {
if (context.getDirection().getReceptionSide().isClient()) {
Player player = Minecraft.getInstance().player;
if (player != null) {
player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY, null)
.ifPresent(levelCap -> {
levelCap.setPowerLevel(message.powerLevel);
});
}
ctx.get().setPacketHandled(true);
}
});
context.setPacketHandled(true);
}
}
}

View File

@@ -3,8 +3,8 @@ package nl.requios.effortlessbuilding.systems;
import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmode.ModeOptions;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.network.IsQuickReplacingPacket;
import nl.requios.effortlessbuilding.network.PacketHandler;
@@ -68,6 +68,6 @@ public class BuildSettings {
}
private boolean canReplaceBlocks(){
return Minecraft.getInstance().player != null && EffortlessBuildingClient.POWER_LEVEL.canReplaceBlocks(Minecraft.getInstance().player);
return Minecraft.getInstance().player != null && CapabilityHandler.canReplaceBlocks(Minecraft.getInstance().player);
}
}

View File

@@ -21,6 +21,7 @@ import nl.requios.effortlessbuilding.ClientConfig;
import nl.requios.effortlessbuilding.ClientEvents;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmode.BuildModeEnum;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.compatibility.CompatHelper;
import nl.requios.effortlessbuilding.item.AbstractRandomizerBagItem;
import nl.requios.effortlessbuilding.network.PacketHandler;
@@ -103,7 +104,8 @@ public class BuilderChain {
}
var player = Minecraft.getInstance().player;
if (player != null && !EffortlessBuildingClient.POWER_LEVEL.canBreakFar(player)) return;
if (player == null) return;
if (!CapabilityHandler.canBreakFar(player)) return;
if (buildingState == BuildingState.IDLE){
buildingState = BuildingState.BREAKING;
@@ -208,7 +210,7 @@ public class BuilderChain {
private BlockEntry findStartPosition(Player player, BuildModeEnum buildMode) {
int maxReach = EffortlessBuildingClient.POWER_LEVEL.getPlacementReach(player);
int maxReach = CapabilityHandler.getPlacementReach(player, false);
//Determine if we should look far or nearby
boolean shouldLookAtNear = buildMode == BuildModeEnum.DISABLED || maxReach < 3;
@@ -241,7 +243,7 @@ public class BuilderChain {
//We can only break
//Do not break far if we are not allowed to
if (!shouldLookAtNear && !EffortlessBuildingClient.POWER_LEVEL.canBreakFar(player)) return null;
if (!shouldLookAtNear && !CapabilityHandler.canBreakFar(player)) return null;
}
var blockEntry = new BlockEntry(startPos);

View File

@@ -1,118 +0,0 @@
package nl.requios.effortlessbuilding.systems;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import nl.requios.effortlessbuilding.CommonConfig;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.network.PacketHandler;
import nl.requios.effortlessbuilding.network.PowerLevelPacket;
@OnlyIn(Dist.CLIENT)
public class PowerLevel {
private int powerLevel;
public int getPowerLevel() {
return powerLevel;
}
public int getNextPowerLevel() {
return Math.min(powerLevel + 1, ServerPowerLevel.MAX_POWER_LEVEL);
}
public void setPowerLevel(int powerLevel) {
this.powerLevel = powerLevel;
EffortlessBuildingClient.BUILD_MODIFIERS.onPowerLevelChanged(powerLevel);
}
public boolean canIncreasePowerLevel() {
return getPowerLevel() < ServerPowerLevel.MAX_POWER_LEVEL;
}
public void increasePowerLevel() {
if (canIncreasePowerLevel()) {
setPowerLevel(getPowerLevel() + 1);
PacketHandler.INSTANCE.sendToServer(new PowerLevelPacket(powerLevel));
}
}
@Deprecated
public int getMaxReach(Player player) {
return getPlacementReach(player);
}
public int getPlacementReach(Player player) {
return getPlacementReach(player, false);
}
public int getPlacementReach(Player player, boolean nextPowerLevel) {
if (player.isCreative()) return CommonConfig.reach.creative.get();
return switch (nextPowerLevel ? getNextPowerLevel() : getPowerLevel()) {
case 1 -> CommonConfig.reach.level1.get();
case 2 -> CommonConfig.reach.level2.get();
case 3 -> CommonConfig.reach.level3.get();
default -> CommonConfig.reach.level0.get();
};
}
//How far away we can detect the second and third click of build modes (distance to player)
public int getBuildModeReach(Player player) {
//A bit further than placement reach, so you can build lines when looking to the side without having to move.
return getPlacementReach(player) + 6;
}
public int getMaxBlocksPlacedAtOnce(Player player) {
return getMaxBlocksPlacedAtOnce(player, false);
}
public int getMaxBlocksPlacedAtOnce(Player player, boolean nextPowerLevel) {
if (player.isCreative()) return CommonConfig.maxBlocksPlacedAtOnce.creative.get();
return switch (nextPowerLevel ? getNextPowerLevel() : getPowerLevel()) {
case 1 -> CommonConfig.maxBlocksPlacedAtOnce.level1.get();
case 2 -> CommonConfig.maxBlocksPlacedAtOnce.level2.get();
case 3 -> CommonConfig.maxBlocksPlacedAtOnce.level3.get();
default -> CommonConfig.maxBlocksPlacedAtOnce.level0.get();
};
}
public int getMaxBlocksPerAxis(Player player) {
return getMaxBlocksPerAxis(player, false);
}
public int getMaxBlocksPerAxis(Player player, boolean nextPowerLevel) {
if (player.isCreative()) return CommonConfig.maxBlocksPerAxis.creative.get();
return switch (nextPowerLevel ? getNextPowerLevel() : getPowerLevel()) {
case 1 -> CommonConfig.maxBlocksPerAxis.level1.get();
case 2 -> CommonConfig.maxBlocksPerAxis.level2.get();
case 3 -> CommonConfig.maxBlocksPerAxis.level3.get();
default -> CommonConfig.maxBlocksPerAxis.level0.get();
};
}
public int getMaxMirrorRadius(Player player) {
return getMaxMirrorRadius(player, false);
}
public int getMaxMirrorRadius(Player player, boolean nextPowerLevel) {
if (player.isCreative()) return CommonConfig.maxMirrorRadius.creative.get();
return switch (getPowerLevel() + (nextPowerLevel ? 1 : 0)) {
case 1 -> CommonConfig.maxMirrorRadius.level1.get();
case 2 -> CommonConfig.maxMirrorRadius.level2.get();
case 3 -> CommonConfig.maxMirrorRadius.level3.get();
default -> CommonConfig.maxMirrorRadius.level0.get();
};
}
public boolean isDisabled(Player player) {
return getMaxBlocksPlacedAtOnce(player) <= 0 || getMaxBlocksPerAxis(player) <= 0;
}
public boolean canBreakFar(Player player) {
return player.isCreative();
}
public boolean canReplaceBlocks(Player player) {
return player.isCreative();
}
}

View File

@@ -2,6 +2,7 @@ package nl.requios.effortlessbuilding.systems;
import net.minecraft.world.entity.player.Player;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
public class ServerBuildState {
private static final String IS_USING_BUILD_MODE_KEY = EffortlessBuilding.MODID + ":isUsingBuildMode";
@@ -25,7 +26,7 @@ public class ServerBuildState {
}
public static boolean isQuickReplacing(Player player) {
if (!EffortlessBuilding.SERVER_POWER_LEVEL.canReplaceBlocks(player)) return false;
if (!CapabilityHandler.canReplaceBlocks(player)) return false;
return player.getPersistentData().contains(IS_QUICK_REPLACING_KEY);
}

View File

@@ -1,34 +0,0 @@
package nl.requios.effortlessbuilding.systems;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.network.PacketDistributor;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.network.PacketHandler;
import nl.requios.effortlessbuilding.network.PowerLevelPacket;
public class ServerPowerLevel {
public static final int MAX_POWER_LEVEL = 3; //Common access
private static final String POWER_LEVEL_KEY = EffortlessBuilding.MODID + ":powerLevel";
public int getPowerLevel(Player player) {
if (!player.getPersistentData().contains(POWER_LEVEL_KEY)) return 0;
return player.getPersistentData().getInt(POWER_LEVEL_KEY);
}
public void setPowerLevel(Player player, int powerLevel) {
player.getPersistentData().putInt(POWER_LEVEL_KEY, powerLevel);
}
public void sendToClient(Player player) {
PacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new PowerLevelPacket(getPowerLevel(player)));
}
public boolean canBreakFar(Player player) {
return player.isCreative();
}
public boolean canReplaceBlocks(Player player) {
return player.isCreative();
}
}

View File

@@ -6,7 +6,7 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.DistExecutor;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
@@ -102,7 +102,7 @@ public class BlockSet extends HashMap<BlockPos, BlockEntry> implements Iterable<
public static class ClientSide {
public static boolean isFull(BlockSet blockSet) {
//Limit number of blocks you can place
int limit = EffortlessBuildingClient.POWER_LEVEL.getMaxBlocksPlacedAtOnce(net.minecraft.client.Minecraft.getInstance().player);
int limit = CapabilityHandler.getMaxBlocksPlacedAtOnce(net.minecraft.client.Minecraft.getInstance().player, false);
if (blockSet.size() >= limit) {
if (logging) EffortlessBuilding.log("BlockSet limit reached, not adding block.");
return true;

View File

@@ -12,7 +12,7 @@ import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
@OnlyIn(Dist.CLIENT)
public class ClientBlockUtilities {
@@ -52,7 +52,7 @@ public class ClientBlockUtilities {
Level world = player.level();
//base distance off of player ability (config)
float raytraceRange = EffortlessBuildingClient.POWER_LEVEL.getPlacementReach(player);
float raytraceRange = CapabilityHandler.getPlacementReach(player, false);
Vec3 look = player.getLookAngle();
Vec3 start = new Vec3(player.getX(), player.getY() + player.getEyeHeight(), player.getZ());

View File

@@ -7,9 +7,14 @@ import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.systems.ServerPowerLevel;
import net.minecraftforge.network.PacketDistributor;
import nl.requios.effortlessbuilding.capability.CapabilityHandler;
import nl.requios.effortlessbuilding.capability.IPowerLevel;
import nl.requios.effortlessbuilding.capability.PowerLevelCapability;
import nl.requios.effortlessbuilding.network.PacketHandler;
import nl.requios.effortlessbuilding.network.PowerLevelPacket;
public class PowerLevelCommand {
@@ -32,7 +37,7 @@ public class PowerLevelCommand {
})))
.then(Commands.literal("set")
.then(Commands.argument("target", EntityArgument.player())
.then(Commands.argument("value", IntegerArgumentType.integer(0, ServerPowerLevel.MAX_POWER_LEVEL)).executes(ctx -> {
.then(Commands.argument("value", IntegerArgumentType.integer(0, PowerLevelCapability.MAX_POWER_LEVEL)).executes(ctx -> {
//Set power level
setPowerLevel(ctx.getSource(), EntityArgument.getPlayer(ctx, "target"), ctx.getArgument("value", Integer.class));
@@ -42,13 +47,16 @@ public class PowerLevelCommand {
}
private static void logPowerLevel(CommandSourceStack source, Player player) {
int powerLevel = EffortlessBuilding.SERVER_POWER_LEVEL.getPowerLevel(player);
int powerLevel = CapabilityHandler.getPowerLevel(player);
source.sendSuccess(() -> Component.translatable("effortlessbuilding.commands.powerlevel", player.getDisplayName(), powerLevel), false);
}
private static void setPowerLevel(CommandSourceStack source, Player player, int powerLevel) throws CommandSyntaxException {
EffortlessBuilding.SERVER_POWER_LEVEL.setPowerLevel(player, powerLevel);
EffortlessBuilding.SERVER_POWER_LEVEL.sendToClient(player);
IPowerLevel powerCap = player.getCapability(CapabilityHandler.POWER_LEVEL_CAPABILITY).orElse(null);
if (powerCap == null) return; //Should never be null but just to be sure
powerCap.setPowerLevel(powerLevel);
PacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new PowerLevelPacket(powerLevel));
source.sendSuccess(() -> Component.translatable("effortlessbuilding.commands.powerlevel.success", player.getDisplayName(), powerLevel), true);
}
}