Skip to content

Commit

Permalink
feat: Allow sorting of sharestones #394
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Dec 16, 2023
1 parent 5250226 commit 751dd1b
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult blockHitResult) {
if (!world.isClientSide) {
final var waystones = PlayerWaystoneManager.getTargetsForPlayer(player);
PlayerWaystoneManager.ensureSortingIndex(player, waystones);
Balm.getNetworking().openGui(player, new BalmMenuProvider() {
@Override
public Component getDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.blay09.mods.waystones.block.entity;

import net.blay09.mods.balm.api.menu.BalmMenuProvider;
import net.blay09.mods.waystones.api.IWaystone;
import net.blay09.mods.waystones.api.WaystoneOrigin;
import net.blay09.mods.waystones.block.SharestoneBlock;
import net.blay09.mods.waystones.core.*;
Expand All @@ -23,9 +22,6 @@
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.stream.Collectors;

public class SharestoneBlockEntity extends WaystoneBlockEntityBase {

public SharestoneBlockEntity(BlockPos pos, BlockState state) {
Expand Down Expand Up @@ -56,6 +52,7 @@ public Component getDisplayName() {
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
final var fromWaystone = getWaystone();
final var waystones = PlayerWaystoneManager.getTargetsForWaystone(player, fromWaystone);
PlayerWaystoneManager.ensureSortingIndex(player, waystones);
return new WaystoneSelectionMenu(ModMenus.sharestoneSelection.get(), WarpMode.SHARESTONE_TO_SHARESTONE, fromWaystone, windowId, waystones);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Component getDisplayName() {
@Override
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
final var waystones = PlayerWaystoneManager.getTargetsForWaystone(player, getWaystone());
PlayerWaystoneManager.ensureSortingIndex(player, waystones);
return new WaystoneSelectionMenu(ModMenus.waystoneSelection.get(), WarpMode.WAYSTONE_TO_WAYSTONE, getWaystone(), windowId, waystones);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public SharestoneSelectionScreen(WaystoneSelectionMenu container, Inventory play

@Override
protected boolean allowSorting() {
return false;
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public abstract class WaystoneSelectionScreenBase extends AbstractContainerScree
public WaystoneSelectionScreenBase(WaystoneSelectionMenu container, Inventory playerInventory, Component title) {
super(container, playerInventory, title);
waystones = container.getWaystones();
PlayerWaystoneManager.ensureSortingIndex(Minecraft.getInstance().player, waystones);
filteredWaystones = new ArrayList<>(waystones);
final var sorting = getSorting();
if (sorting != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface IPlayerWaystoneData {
long getInventoryButtonCooldownUntil(Player player);
void setInventoryButtonCooldownUntil(Player player, long timeStamp);
List<UUID> getSortingIndex(Player player);
List<UUID> ensureSortingIndex(Player player, Collection<IWaystone> waystones);
void setSortingIndex(Player player, List<UUID> sortingIndex);
Collection<IWaystone> getWaystones(Player player);
void sortWaystoneAsFirst(Player player, UUID waystoneUid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ public List<UUID> getSortingIndex(Player player) {
return sortingIndex;
}

@Override
public List<UUID> ensureSortingIndex(Player player, Collection<IWaystone> waystones) {
final var existing = new HashSet<>(sortingIndex);

for (final var waystone : waystones) {
final var waystoneUid = waystone.getWaystoneUid();
if (!existing.contains(waystoneUid)) {
sortingIndex.add(waystoneUid);
}
}

return sortingIndex;
}

@Override
public void setSortingIndex(Player player, List<UUID> sortingIndex) {
this.sortingIndex.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ public void setSortingIndex(Player player, List<UUID> sortingIndex) {
}
}

@Override
public List<UUID> ensureSortingIndex(Player player, Collection<IWaystone> waystones) {
final var sortingIndexData = getSortingIndexData(getWaystonesData(player));
final var sortingIndex = new ArrayList<UUID>();
final var existing = new HashSet<UUID>();
for (final var sortingIndexEntry : sortingIndexData) {
final var waystoneUid = UUID.fromString(sortingIndexEntry.getAsString());
if (existing.add(waystoneUid)) {
sortingIndex.add(waystoneUid);
}
}

for (final var waystone : waystones) {
final var waystoneUid = waystone.getWaystoneUid();
if (!existing.contains(waystoneUid)) {
sortingIndex.add(waystoneUid);
sortingIndexData.add(StringTag.valueOf(waystoneUid.toString()));
}
}

return sortingIndex;
}

@Override
public void sortWaystoneAsFirst(Player player, UUID waystoneUid) {
final var sortingIndex = getSortingIndexData(getWaystonesData(player));
Expand Down Expand Up @@ -107,11 +130,9 @@ public void sortWaystoneSwap(Player player, UUID waystoneUid, UUID otherWaystone
}
}

System.out.println(sortingIndex);
if (waystoneIndex != -1 && otherWaystoneIndex != -1) {
Collections.swap(sortingIndex, waystoneIndex, otherWaystoneIndex);
}
System.out.println(sortingIndex);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ public static List<UUID> getSortingIndex(Player player) {
return getPlayerWaystoneData(player.level()).getSortingIndex(player);
}

public static List<UUID> ensureSortingIndex(Player player, Collection<IWaystone> waystones) {
return getPlayerWaystoneData(player.level()).ensureSortingIndex(player, waystones);
}

public static void sortWaystoneAsFirst(Player player, UUID waystoneUid) {
getPlayerWaystoneData(player.level()).sortWaystoneAsFirst(player, waystoneUid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public int getUseDuration(ItemStack itemStack) {
public ItemStack finishUsingItem(ItemStack itemStack, Level world, LivingEntity entity) {
if (!world.isClientSide && entity instanceof ServerPlayer player) {
final var waystones = PlayerWaystoneManager.getTargetsForItem(player, itemStack);
PlayerWaystoneManager.ensureSortingIndex(player, waystones);
Balm.getNetworking().openGui(((ServerPlayer) entity), new BalmMenuProvider() {
@Override
public Component getDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public void onUseTick(Level level, LivingEntity entity, ItemStack itemStack, int
public ItemStack finishUsingItem(ItemStack itemStack, Level world, LivingEntity entity) {
if (!world.isClientSide && entity instanceof ServerPlayer player) {
final var waystones = PlayerWaystoneManager.getTargetsForItem(player, itemStack);
PlayerWaystoneManager.ensureSortingIndex(player, waystones);
Balm.getNetworking().openGui(player, new BalmMenuProvider() {
@Override
public Component getDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static void handle(ServerPlayer player, InventoryButtonMessage message) {
WaystoneTeleportManager.tryTeleportToWaystone(player, waystone, WarpMode.INVENTORY_BUTTON, null);
} else if (inventoryButtonMode.isReturnToAny()) {
final var waystones = PlayerWaystoneManager.getTargetsForInventoryButton(player);
PlayerWaystoneManager.ensureSortingIndex(player, waystones);
final BalmMenuProvider containerProvider = new BalmMenuProvider() {
@Override
public Component getDisplayName() {
Expand Down

0 comments on commit 751dd1b

Please sign in to comment.