Skip to content

Commit

Permalink
refactor: Cleanup and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Dec 16, 2023
1 parent 4a926a0 commit 6b8f745
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IMutableWaystone {
/**
* @deprecated Use {@link #setVisibility(WaystoneVisibility)} instead.
*/
@Deprecated
@Deprecated(forRemoval = true)
void setGlobal(boolean global);

void setVisibility(WaystoneVisibility visibility);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ default boolean wasGenerated() {
/**
* @deprecated Use {@link #getVisibility()} instead.
*/
@Deprecated
@Deprecated(forRemoval = true)
boolean isGlobal();

boolean isOwner(Player player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public enum WaystoneVisibility {
ACTIVATION,
GLOBAL
GLOBAL,
SHARD_ONLY
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.mixin.ScreenAccessor;
import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.api.WaystoneTypes;
import net.blay09.mods.waystones.api.WaystoneVisibility;
import net.blay09.mods.waystones.client.gui.widget.ITooltipProvider;
import net.blay09.mods.waystones.client.gui.widget.RemoveWaystoneButton;
import net.blay09.mods.waystones.client.gui.widget.SortWaystoneButton;
Expand Down Expand Up @@ -166,18 +168,15 @@ private void updateList() {
addRenderableWidget(sortDownButton);
}

if (allowDeletion()) {
if (allowDeletion(waystone)) {
RemoveWaystoneButton removeButton = new RemoveWaystoneButton(width / 2 + 122, y + 4, y, 20, waystone, button -> {
Player player = Minecraft.getInstance().player;
PlayerWaystoneManager.deactivateWaystone(Objects.requireNonNull(player), waystone);
waystones.remove(waystone);
Balm.getNetworking().sendToServer(new RemoveWaystoneMessage(waystone.getWaystoneUid()));
updateList();
});
// Only show the remove button for non-global waystones, or if the player is in creative mode
if (!waystone.isGlobal() || Minecraft.getInstance().player.getAbilities().instabuild) {
addRenderableWidget(removeButton);
}
addRenderableWidget(removeButton);
}

y += 22;
Expand All @@ -188,6 +187,18 @@ private void updateList() {
btnNextPage.setY(topPos + headerY + headerHeight + buttonsPerPage * 22 + (filteredWaystones.size() > 0 ? 10 : 0));
}

private boolean allowDeletion(IWaystone waystone) {
if (waystone.getVisibility() == WaystoneVisibility.GLOBAL && !Minecraft.getInstance().player.getAbilities().instabuild) {
return false;
}

if (!waystone.getWaystoneType().equals(WaystoneTypes.WAYSTONE)) {
return false;
}

return allowDeletion();
}

private WaystoneButton createWaystoneButton(int y, final IWaystone waystone) {
IWaystone waystoneFrom = menu.getWaystoneFrom();
Player player = Minecraft.getInstance().player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void init() {
addRenderableWidget(textField);
setInitialFocus(textField);

visibilityButton = new WaystoneVisbilityButton(leftPos + 9, topPos + 20, oldVisibility);
visibilityButton = new WaystoneVisbilityButton(leftPos + 9, topPos + 20, oldVisibility, menu.getVisibilityOptions());
addRenderableWidget(visibilityButton);
}

Expand Down Expand Up @@ -133,12 +133,12 @@ protected void renderBg(GuiGraphics guiGraphics, float partialTicks, int mouseX,

@Override
public void onClose() {
super.onClose();

if (textField != null && visibilityButton != null) {
Balm.getNetworking()
.sendToServer(new EditWaystoneMessage(menu.getWaystone().getWaystoneUid(), textField.getValue(), visibilityButton.getVisibility()));
}

super.onClose();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Lists;
import com.mojang.blaze3d.systems.RenderSystem;
import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.api.WaystoneVisibility;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
Expand All @@ -28,7 +29,7 @@ public RemoveWaystoneButton(int x, int y, int visibleRegionStart, int visibleReg
this.visibleRegionHeight = visibleRegionHeight;
tooltip = Lists.newArrayList(Component.translatable("gui.waystones.waystone_selection.hold_shift_to_delete"));
activeTooltip = Lists.newArrayList(Component.translatable("gui.waystones.waystone_selection.click_to_delete"));
if (waystone.isGlobal()) {
if (waystone.getVisibility() == WaystoneVisibility.GLOBAL) {
var component = Component.translatable("gui.waystones.waystone_selection.deleting_global_for_all");
component.withStyle(ChatFormatting.DARK_RED);
tooltip.add(component);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.systems.RenderSystem;
import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.api.WaystoneVisibility;
import net.blay09.mods.waystones.core.PlayerWaystoneManager;
import net.blay09.mods.waystones.api.ExperienceCost;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -46,7 +47,7 @@ private static Component getWaystoneNameComponent(IWaystone waystone) {
effectiveName = I18n.get("gui.waystones.waystone_selection.unnamed_waystone");
}
final var textComponent = Component.literal(effectiveName);
if (waystone.isGlobal()) {
if (waystone.getVisibility() == WaystoneVisibility.GLOBAL) {
textComponent.withStyle(ChatFormatting.YELLOW);
}
return textComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public class WaystoneVisbilityButton extends Button implements ITooltipProvider

private static final ResourceLocation WAYSTONE_GUI_TEXTURES = new ResourceLocation(Waystones.MOD_ID, "textures/gui/menu/waystone.png");

private final List<WaystoneVisibility> options;
private WaystoneVisibility visibility;

public WaystoneVisbilityButton(int x, int y, WaystoneVisibility visibility) {
public WaystoneVisbilityButton(int x, int y, WaystoneVisibility visibility, List<WaystoneVisibility> options) {
super(x, y, 18, 18, Component.empty(), button -> {
}, Button.DEFAULT_NARRATION);
this.options = options;
this.visibility = visibility;
}

Expand Down Expand Up @@ -48,6 +50,7 @@ public WaystoneVisibility getVisibility() {

@Override
public void onPress() {
visibility = WaystoneVisibility.values()[(visibility.ordinal() + 1) % WaystoneVisibility.values().length];
final var index = options.indexOf(visibility);
visibility = options.get((index + 1) % options.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static boolean canUseWarpStone(Player player, ItemStack heldItem) {
}

public static double getCooldownMultiplier(IWaystone waystone) {
return waystone.isGlobal() ? WaystonesConfig.getActive().cooldowns.globalWaystoneCooldownMultiplier : 1f;
return waystone.getVisibility() == WaystoneVisibility.GLOBAL ? WaystonesConfig.getActive().cooldowns.globalWaystoneCooldownMultiplier : 1f;
}

private static void informPlayer(Entity entity, String translationKey) {
Expand Down Expand Up @@ -147,7 +147,7 @@ public static boolean canDimensionalWarpBetween(Entity player, IWaystone wayston
}

DimensionalWarp dimensionalWarpMode = WaystonesConfig.getActive().restrictions.dimensionalWarp;
return dimensionalWarpMode == DimensionalWarp.ALLOW || dimensionalWarpMode == DimensionalWarp.GLOBAL_ONLY && waystone.isGlobal();
return dimensionalWarpMode == DimensionalWarp.ALLOW || dimensionalWarpMode == DimensionalWarp.GLOBAL_ONLY && waystone.getVisibility() == WaystoneVisibility.GLOBAL;
}

public static ItemStack findWarpItem(Entity entity, WarpMode warpMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ public static IWaystone read(FriendlyByteBuf buf) {
UUID waystoneUid = buf.readUUID();
ResourceLocation waystoneType = buf.readResourceLocation();
String name = buf.readUtf();
boolean isGlobal = buf.readBoolean();
final var visibility = buf.readEnum(WaystoneVisibility.class);
ResourceKey<Level> dimension = ResourceKey.create(Registries.DIMENSION, new ResourceLocation(buf.readUtf(250)));
BlockPos pos = buf.readBlockPos();
WaystoneOrigin origin = buf.readEnum(WaystoneOrigin.class);

Waystone waystone = new Waystone(waystoneType, waystoneUid, dimension, pos, origin, null);
waystone.setName(name);
waystone.setGlobal(isGlobal);
waystone.setVisibility(visibility);
return waystone;
}

Expand All @@ -198,7 +198,11 @@ public static IWaystone read(CompoundTag compound) {
ResourceLocation waystoneType = compound.contains("Type") ? new ResourceLocation(compound.getString("Type")) : WaystoneTypes.WAYSTONE;
Waystone waystone = new Waystone(waystoneType, waystoneUid, dimensionType, pos, origin, ownerUid);
waystone.setName(name);
waystone.setGlobal(compound.getBoolean("IsGlobal"));
if (compound.contains("Visibility")) {
waystone.setVisibility(WaystoneVisibility.valueOf(compound.getString("Visibility")));
} else {
waystone.setVisibility(compound.getBoolean("IsGlobal") ? WaystoneVisibility.GLOBAL : WaystoneVisibility.ACTIVATION);
}
return waystone;
}

Expand All @@ -213,7 +217,7 @@ public static void write(FriendlyByteBuf buf, IWaystone waystone) {
buf.writeUUID(waystone.getWaystoneUid());
buf.writeResourceLocation(waystone.getWaystoneType());
buf.writeUtf(waystone.getName());
buf.writeBoolean(waystone.isGlobal());
buf.writeEnum(waystone.getVisibility());
buf.writeResourceLocation(waystone.getDimension().location());
buf.writeBlockPos(waystone.getPos());
buf.writeEnum(waystone.getOrigin());
Expand All @@ -229,7 +233,7 @@ public static CompoundTag write(IWaystone waystone, CompoundTag compound) {
if (waystone.getOwnerUid() != null) {
compound.put("OwnerUid", NbtUtils.createUUID(waystone.getOwnerUid()));
}
compound.putBoolean("IsGlobal", waystone.isGlobal());
compound.putString("Visibility", waystone.getVisibility().name());
return compound;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void addWaystone(IWaystone waystone) {
public void updateWaystone(IWaystone waystone) {
Waystone mutableWaystone = (Waystone) waystones.getOrDefault(waystone.getWaystoneUid(), waystone);
mutableWaystone.setName(waystone.getName());
mutableWaystone.setGlobal(waystone.isGlobal());
mutableWaystone.setVisibility(waystone.getVisibility());
waystones.put(waystone.getWaystoneUid(), mutableWaystone);
setDirty();
Balm.getEvents().fireEvent(new WaystoneUpdatedEvent(waystone));
Expand Down Expand Up @@ -85,7 +85,7 @@ public Stream<IWaystone> getWaystonesByType(ResourceLocation type) {

@Override
public List<IWaystone> getGlobalWaystones() {
return waystones.values().stream().filter(IWaystone::isGlobal).collect(Collectors.toList());
return waystones.values().stream().filter(it -> it.getVisibility() == WaystoneVisibility.GLOBAL).collect(Collectors.toList());
}

public static WaystoneManager read(CompoundTag tagCompound) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.blay09.mods.waystones.core;

import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.api.WaystoneVisibility;
import net.blay09.mods.waystones.config.WaystonesConfig;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
Expand All @@ -22,7 +23,7 @@ public static boolean mayBreakWaystone(Player player, BlockGetter world, BlockPo
return false;
}

boolean isGlobal = waystone.isGlobal();
boolean isGlobal = waystone.getVisibility() == WaystoneVisibility.GLOBAL;
boolean mayBreakGlobalWaystones = !WaystonesConfig.getActive().restrictions.globalWaystoneSetupRequiresCreativeMode;
return !isGlobal || mayBreakGlobalWaystones;
}
Expand All @@ -46,7 +47,7 @@ public static WaystoneEditPermissions mayEditWaystone(Player player, Level world
return WaystoneEditPermissions.NOT_THE_OWNER;
}

if (waystone.isGlobal() && !player.getAbilities().instabuild && WaystonesConfig.getActive().restrictions.globalWaystoneSetupRequiresCreativeMode) {
if (waystone.getVisibility() == WaystoneVisibility.GLOBAL && !player.getAbilities().instabuild && WaystonesConfig.getActive().restrictions.globalWaystoneSetupRequiresCreativeMode) {
return WaystoneEditPermissions.GET_CREATIVE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static int getExperienceLevelCost(Entity entity, IWaystone waystone, Warp
int xpForLeashed = WaystonesConfig.getActive().xpCost.xpCostPerLeashed * context.getLeashedEntities().size();

double xpCostMultiplier = warpMode.getXpCostMultiplier();
if (waystone.isGlobal()) {
if (waystone.getVisibility() == WaystoneVisibility.GLOBAL) {
xpCostMultiplier *= WaystonesConfig.getActive().xpCost.globalWaystoneXpCostMultiplier;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.blay09.mods.waystones.menu;

import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.api.WaystoneTypes;
import net.blay09.mods.waystones.api.WaystoneVisibility;
import net.blay09.mods.waystones.block.entity.WaystoneBlockEntityBase;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Inventory;
Expand All @@ -10,6 +12,8 @@
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;

import java.util.List;

public class WaystoneSettingsMenu extends AbstractContainerMenu {

private final IWaystone waystone;
Expand Down Expand Up @@ -89,4 +93,16 @@ public boolean stillValid(Player player) {
public IWaystone getWaystone() {
return waystone;
}

public List<WaystoneVisibility> getVisibilityOptions() {
if (WaystoneTypes.isSharestone(waystone.getWaystoneType())) {
return List.of(WaystoneVisibility.GLOBAL);
} else if(waystone.getWaystoneType().equals(WaystoneTypes.WARP_PLATE)) {
return List.of(WaystoneVisibility.SHARD_ONLY);
} else if(waystone.getWaystoneType().equals(WaystoneTypes.LANDING_STONE)) {
return List.of(WaystoneVisibility.SHARD_ONLY);
} else {
return List.of(WaystoneVisibility.ACTIVATION, WaystoneVisibility.GLOBAL);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package net.blay09.mods.waystones.network.message;

import net.blay09.mods.waystones.Waystones;
import net.blay09.mods.waystones.api.WaystoneTypes;
import net.blay09.mods.waystones.api.WaystoneVisibility;
import net.blay09.mods.waystones.config.WaystonesConfig;
import net.blay09.mods.waystones.core.*;
import net.blay09.mods.waystones.menu.WaystoneSettingsMenu;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -43,6 +46,22 @@ public static void handle(ServerPlayer player, EditWaystoneMessage message) {
return;
}

if (!(player.containerMenu instanceof WaystoneSettingsMenu settingsMenu)) {
return;
}

var visibility = message.visibility;
if (!settingsMenu.getVisibilityOptions().contains(message.visibility)) {
Waystones.logger.warn("{} tried to edit a waystone with an invalid visibility {}", player.getName().getString(), message.visibility);
visibility = settingsMenu.getVisibilityOptions().get(0);
}

if (visibility == WaystoneVisibility.GLOBAL && waystone.getWaystoneType()
.equals(WaystoneTypes.WAYSTONE) && !PlayerWaystoneManager.mayEditGlobalWaystones(player)) {
Waystones.logger.warn("{} tried to edit a global waystone without permission", player.getName().getString());
return;
}

BlockPos pos = waystone.getPos();
if (player.distanceToSqr(pos.getX() + 0.5f, pos.getY() + 0.5f, pos.getZ() + 0.5f) > 64) {
return;
Expand All @@ -53,11 +72,11 @@ public static void handle(ServerPlayer player, EditWaystoneMessage message) {
backingWaystone.setName(legalName);

if (PlayerWaystoneManager.mayEditGlobalWaystones(player)) {
if (!backingWaystone.isGlobal() && message.visibility == WaystoneVisibility.GLOBAL) {
if (backingWaystone.getVisibility() != WaystoneVisibility.GLOBAL && visibility == WaystoneVisibility.GLOBAL) {
PlayerWaystoneManager.activeWaystoneForEveryone(player.server, backingWaystone);
}
backingWaystone.setGlobal(message.visibility == WaystoneVisibility.GLOBAL);
}
backingWaystone.setVisibility(visibility);

WaystoneManager.get(player.server).setDirty();
WaystoneSyncManager.sendWaystoneUpdateToAll(player.server, backingWaystone);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.blay09.mods.waystones.network.message;

import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.api.WaystoneVisibility;
import net.blay09.mods.waystones.block.WaystoneBlock;
import net.blay09.mods.waystones.core.*;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -34,10 +35,10 @@ public static void handle(ServerPlayer player, RemoveWaystoneMessage message) {
PlayerWaystoneManager.deactivateWaystone(player, waystone);

// If the waystone is global and the player is in creative mode, remove the global-ness
if (waystone.isGlobal() && player.getAbilities().instabuild) {
if (waystone.getVisibility() == WaystoneVisibility.GLOBAL && player.getAbilities().instabuild) {
IWaystone backingWaystone = waystone.getBackingWaystone();
if (backingWaystone instanceof Waystone) {
((Waystone) backingWaystone).setGlobal(false);
((Waystone) backingWaystone).setVisibility(WaystoneVisibility.ACTIVATION);

// Check if the waystone block still exists - if not, completely remove the waystone from existence to remove it from all players
// This way we can't have orphan global waystones left over. And just in case the waystone *was* just being silk-touch moved, it's easy to reactivate a global waystone for everyone (since it does that automatically).
Expand Down
1 change: 1 addition & 0 deletions shared/src/main/resources/assets/waystones/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"tooltip.waystones.visibility": "Visibility: %s",
"tooltip.waystones.visibility.activation": "On Activation",
"tooltip.waystones.visibility.global": "Global",
"tooltip.waystones.visibility.shard_only": "Shards only",
"stat.waystones.waystone_activated": "Waystones Activated",
"waystones.untitled_waystone": "Untitled Waystone",
"waystones:warp_plate": "Warp Plate",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6b8f745

Please sign in to comment.