Skip to content

Commit

Permalink
refactor: Restructure some things, prepare WaystoneUserSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Dec 16, 2023
1 parent 38c8678 commit 405e440
Show file tree
Hide file tree
Showing 16 changed files with 439 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@
import net.blay09.mods.waystones.block.WaystoneBlock;
import net.blay09.mods.waystones.config.WaystonesConfig;
import net.blay09.mods.waystones.config.WaystonesConfigData;
import net.blay09.mods.waystones.core.PlayerWaystoneManager;
import net.blay09.mods.waystones.core.WarpMode;
import net.blay09.mods.waystones.core.WaystoneManager;
import net.blay09.mods.waystones.core.WaystoneTeleportContext;
import net.blay09.mods.waystones.item.AttunedShardItem;
import net.blay09.mods.waystones.item.BoundScrollItem;
import net.blay09.mods.waystones.core.*;
import net.blay09.mods.waystones.item.ModItems;
import net.minecraft.core.BlockPos;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
Expand All @@ -38,15 +32,15 @@ public Either<IWaystoneTeleportContext, WaystoneTeleportError> createDefaultTele
context.setWarpMode(warpMode);
final var shouldTransportPets = WaystonesConfig.getActive().restrictions.transportPets;
if (shouldTransportPets == WaystonesConfigData.TransportPets.ENABLED || (shouldTransportPets == WaystonesConfigData.TransportPets.SAME_DIMENSION && !context.isDimensionalTeleport())) {
context.getAdditionalEntities().addAll(PlayerWaystoneManager.findPets(entity));
context.getAdditionalEntities().addAll(WaystoneTeleportManager.findPets(entity));
}
context.getLeashedEntities().addAll(PlayerWaystoneManager.findLeashedAnimals(entity));
context.getLeashedEntities().addAll(WaystoneTeleportManager.findLeashedAnimals(entity));
context.setFromWaystone(fromWaystone);
context.setWarpItem(PlayerWaystoneManager.findWarpItem(entity, warpMode));
context.setCooldown(PlayerWaystoneManager.getCooldownPeriod(warpMode, waystone));

// Use the context so far to determine the xp cost
context.setExperienceCost(PlayerWaystoneManager.getExperienceLevelCost(context));
context.setExperienceCost(WaystoneTeleportManager.getExperienceLevelCost(context));
});
}

Expand Down Expand Up @@ -75,12 +69,12 @@ public Either<IWaystoneTeleportContext, WaystoneTeleportError> createCustomTelep

@Override
public Either<List<Entity>, WaystoneTeleportError> tryTeleportToWaystone(Entity entity, IWaystone waystone, WarpMode warpMode, IWaystone fromWaystone) {
return PlayerWaystoneManager.tryTeleportToWaystone(entity, waystone, warpMode, fromWaystone);
return WaystoneTeleportManager.tryTeleportToWaystone(entity, waystone, warpMode, fromWaystone);
}

@Override
public Either<List<Entity>, WaystoneTeleportError> tryTeleport(IWaystoneTeleportContext context) {
return PlayerWaystoneManager.tryTeleport(context);
return WaystoneTeleportManager.tryTeleport(context);
}

@Override
Expand All @@ -90,7 +84,7 @@ public Either<List<Entity>, WaystoneTeleportError> forceTeleportToWaystone(Entit

@Override
public List<Entity> forceTeleport(IWaystoneTeleportContext context) {
return PlayerWaystoneManager.doTeleport(context);
return WaystoneTeleportManager.doTeleport(context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt

@Override
public float getDestroyProgress(BlockState state, Player player, BlockGetter world, BlockPos pos) {
if (!PlayerWaystoneManager.mayBreakWaystone(player, world, pos)) {
if (!WaystonePermissionManager.mayBreakWaystone(player, world, pos)) {
return -1f;
}

Expand All @@ -154,7 +154,7 @@ public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
if (!PlayerWaystoneManager.mayPlaceWaystone(context.getPlayer())) {
if (!WaystonePermissionManager.mayPlaceWaystone(context.getPlayer())) {
return null;
}

Expand Down Expand Up @@ -194,7 +194,7 @@ protected void notifyObserversOfAction(Level world, BlockPos pos) {
@Nullable
protected InteractionResult handleEditActions(Level world, Player player, WaystoneBlockEntityBase tileEntity, IWaystone waystone) {
if (player.isShiftKeyDown()) {
WaystoneEditPermissions result = PlayerWaystoneManager.mayEditWaystone(player, world, waystone);
WaystoneEditPermissions result = WaystonePermissionManager.mayEditWaystone(player, world, waystone);
if (result != WaystoneEditPermissions.ALLOW) {
if (result.getLangKey() != null) {
var chatComponent = Component.translatable(result.getLangKey());
Expand Down Expand Up @@ -360,7 +360,7 @@ public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable L
if (!world.isClientSide && placer instanceof ServerPlayer) {
final ServerPlayer player = (ServerPlayer) placer;
final WaystoneBlockEntityBase waystoneTileEntity = (WaystoneBlockEntityBase) blockEntity;
WaystoneEditPermissions result = PlayerWaystoneManager.mayEditWaystone(player, world, waystoneTileEntity.getWaystone());
WaystoneEditPermissions result = WaystonePermissionManager.mayEditWaystone(player, world, waystoneTileEntity.getWaystone());
if (result == WaystoneEditPermissions.ALLOW) {
MenuProvider settingsContainerProvider = waystoneTileEntity.getSettingsMenuProvider();
if (settingsContainerProvider != null && shouldOpenMenuWhenPlaced()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void onEntityCollision(Entity entity) {
final var status = targetWaystone.filter(IWaystone::isValid)
.map(it -> WarpPlateBlock.WarpPlateStatus.ACTIVE)
.orElse(WarpPlateBlock.WarpPlateStatus.INVALID);
final var canAfford = targetWaystone.map(it -> PlayerWaystoneManager.predictExperienceLevelCost(entity, it, WarpMode.WARP_PLATE, getWaystone()))
final var canAfford = targetWaystone.map(it -> WaystoneTeleportManager.predictExperienceLevelCost(entity, it, WarpMode.WARP_PLATE, getWaystone()))
.map(it -> !(entity instanceof Player player) || player.getAbilities().instabuild || it.canAfford(player))
.orElse(true);
level.setBlock(worldPosition, getBlockState()
Expand Down Expand Up @@ -289,7 +289,7 @@ private void teleportToTarget(Entity entity, IWaystone targetWaystone, ItemStack
.flatMap(ctx -> {
ctx.setWarpItem(targetAttunementStack);
ctx.setConsumesWarpItem(targetAttunementStack.is(ModItemTags.SINGLE_USE_WARP_SHARDS));
return PlayerWaystoneManager.tryTeleport(ctx);
return WaystoneTeleportManager.tryTeleport(ctx);
})
.ifRight(informRejectedTeleport(entity))
.ifLeft(entities -> entities.forEach(this::applyWarpPlateEffects))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.blay09.mods.waystones.config.WaystonesConfig;
import net.blay09.mods.waystones.core.PlayerWaystoneManager;
import net.blay09.mods.waystones.core.WarpMode;
import net.blay09.mods.waystones.core.WaystoneTeleportManager;
import net.blay09.mods.waystones.network.message.InventoryButtonMessage;
import net.blay09.mods.waystones.api.ExperienceCost;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -99,8 +100,8 @@ public static void initialize() {
}

long timeLeft = PlayerWaystoneManager.getInventoryButtonCooldownLeft(player);
IWaystone waystone = PlayerWaystoneManager.getInventoryButtonWaystone(player);
final ExperienceCost xpCost = waystone != null ? PlayerWaystoneManager.predictExperienceLevelCost(player,
IWaystone waystone = PlayerWaystoneManager.getInventoryButtonTarget(player);
final ExperienceCost xpCost = waystone != null ? WaystoneTeleportManager.predictExperienceLevelCost(player,
waystone,
WarpMode.INVENTORY_BUTTON,
null) : ExperienceCost.NoExperienceCost.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.blay09.mods.waystones.client.gui.widget.RemoveWaystoneButton;
import net.blay09.mods.waystones.client.gui.widget.SortWaystoneButton;
import net.blay09.mods.waystones.client.gui.widget.WaystoneButton;
import net.blay09.mods.waystones.core.WaystonePermissionManager;
import net.blay09.mods.waystones.core.WaystoneTeleportManager;
import net.blay09.mods.waystones.menu.WaystoneSelectionMenu;
import net.blay09.mods.waystones.core.PlayerWaystoneManager;
import net.blay09.mods.waystones.core.WaystoneEditPermissions;
Expand Down Expand Up @@ -178,7 +180,7 @@ private void updateList() {
private WaystoneButton createWaystoneButton(int y, final IWaystone waystone) {
IWaystone waystoneFrom = menu.getWaystoneFrom();
Player player = Minecraft.getInstance().player;
final var xpCost = PlayerWaystoneManager.predictExperienceLevelCost(Objects.requireNonNull(player),
final var xpCost = WaystoneTeleportManager.predictExperienceLevelCost(Objects.requireNonNull(player),
waystone,
menu.getWarpMode(),
waystoneFrom);
Expand Down Expand Up @@ -278,7 +280,7 @@ private void drawLocationHeader(GuiGraphics guiGraphics, IWaystone waystone, int
&& mouseY >= startY && mouseY < startY + font.lineHeight;

Player player = Minecraft.getInstance().player;
WaystoneEditPermissions waystoneEditPermissions = PlayerWaystoneManager.mayEditWaystone(player, player.level(), waystone);
WaystoneEditPermissions waystoneEditPermissions = WaystonePermissionManager.mayEditWaystone(player, player.level(), waystone);

String fullText = locationPrefix + ChatFormatting.WHITE;
if (isLocationHeaderHovered && waystoneEditPermissions == WaystoneEditPermissions.ALLOW) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.blay09.mods.waystones.comparator;

import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.core.PlayerWaystoneManager;
import net.minecraft.world.entity.player.Player;

import java.util.Comparator;

public class UserSortingComparator implements Comparator<IWaystone> {

private final Player player;

public UserSortingComparator(Player player) {
this.player = player;
}

@Override
public int compare(IWaystone o1, IWaystone o2) {
final var settings1 = PlayerWaystoneManager.getUserSettingsForWaystone(player, o1);
final var settings2 = PlayerWaystoneManager.getUserSettingsForWaystone(player, o2);
final var sortIndex1 = settings1.sortIndex();
final var sortIndex2 = settings2.sortIndex();
return Integer.compare(sortIndex1, sortIndex2);
}
}
Loading

0 comments on commit 405e440

Please sign in to comment.