Skip to content

Commit

Permalink
Make GuiSecurityTab's ISecurityObject properly "update" when the held…
Browse files Browse the repository at this point in the history
… stack changes to properly update what level it displays mekanism#6993
  • Loading branch information
pupnewfster committed Feb 16, 2021
1 parent 7910add commit ebf355c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ public GuiSecurityTab(IGuiWrapper gui, ISecurityObject securityObject, int y) {
}

private static ISecurityObject getItemSecurityObject(@Nonnull Hand hand) {
ItemStack stack = minecraft.player.getHeldItem(hand);
if (stack.isEmpty() || !(stack.getItem() instanceof ISecurityItem)) {
minecraft.player.closeScreen();
return ISecurityObject.NO_SECURITY;
}
return SecurityUtils.wrapSecurityItem(stack);
return SecurityUtils.wrapSecurityItem(() -> {
ItemStack stack = minecraft.player.getHeldItem(hand);
if (stack.isEmpty() || !(stack.getItem() instanceof ISecurityItem)) {
minecraft.player.closeScreen();
return ItemStack.EMPTY;
}
return stack;
});
}

public GuiSecurityTab(IGuiWrapper gui, @Nonnull Hand hand) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

public interface ISecurityContainer {

/**
* @apiNote Only for use on the server, which means that it doesn't need to properly update on the client side if the stack changes
*/
ISecurityObject getSecurityObject();
}
60 changes: 60 additions & 0 deletions src/main/java/mekanism/common/util/SecurityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import mekanism.api.text.EnumColor;
Expand Down Expand Up @@ -188,4 +189,63 @@ public void setSecurityMode(SecurityMode mode) {
}
};
}

/**
* @apiNote Mainly for use on the client side when the stack potentially could change while our security object currently exists
*/
public static ISecurityObject wrapSecurityItem(@Nonnull Supplier<ItemStack> stackSupplier) {
ItemStack stack = stackSupplier.get();
if (stack.isEmpty() || !(stack.getItem() instanceof ISecurityItem)) {
return ISecurityObject.NO_SECURITY;
}
return new ISecurityObject() {

private ItemStack getAndValidateStack() {
ItemStack stack = stackSupplier.get();
if (stack.isEmpty() || !(stack.getItem() instanceof ISecurityItem)) {
return ItemStack.EMPTY;
}
return stack;
}

@Override
public boolean hasSecurity() {
return !getAndValidateStack().isEmpty();
}

@Nullable
@Override
public UUID getOwnerUUID() {
ItemStack stack = getAndValidateStack();
if (stack.isEmpty()) {
return null;
}
return ((ISecurityItem) stack.getItem()).getOwnerUUID(stack);
}

@Nullable
@Override
public String getOwnerName() {
UUID ownerUUID = getOwnerUUID();
return ownerUUID == null ? null : MekanismClient.clientUUIDMap.get(ownerUUID);
}

@Override
public SecurityMode getSecurityMode() {
ItemStack stack = getAndValidateStack();
if (stack.isEmpty()) {
return SecurityMode.PUBLIC;
}
return ((ISecurityItem) stack.getItem()).getSecurity(stack);
}

@Override
public void setSecurityMode(SecurityMode mode) {
ItemStack stack = getAndValidateStack();
if (!stack.isEmpty()) {
((ISecurityItem) stack.getItem()).setSecurity(stack, mode);
}
}
};
}
}

0 comments on commit ebf355c

Please sign in to comment.