change gui data to generics so its safer

This commit is contained in:
Flemmli97 2021-06-13 16:03:51 +02:00
parent 5c4af308bb
commit 2d3108f4c5
10 changed files with 65 additions and 58 deletions

View File

@ -0,0 +1,10 @@
package io.github.flemmli97.flan.gui;
import io.github.flemmli97.flan.claim.Claim;
interface ClaimGroup {
Claim getClaim();
String getGroup();
}

View File

@ -20,7 +20,7 @@ import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
public class ClaimMenuScreenHandler extends ServerOnlyScreenHandler { public class ClaimMenuScreenHandler extends ServerOnlyScreenHandler<Claim> {
private final Claim claim; private final Claim claim;
@ -45,8 +45,7 @@ public class ClaimMenuScreenHandler extends ServerOnlyScreenHandler {
} }
@Override @Override
protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData) { protected void fillInventoryWith(PlayerEntity player, Inventory inv, Claim claim) {
Claim claim = (Claim) additionalData[0];
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
switch (i) { switch (i) {
case 0: case 0:

View File

@ -16,12 +16,12 @@ import net.minecraft.util.Formatting;
import java.util.function.Consumer; import java.util.function.Consumer;
public class ConfirmScreenHandler extends ServerOnlyScreenHandler { public class ConfirmScreenHandler extends ServerOnlyScreenHandler<Object> {
private final Consumer<Boolean> cons; private final Consumer<Boolean> cons;
private ConfirmScreenHandler(int syncId, PlayerInventory playerInventory, Consumer<Boolean> cons) { private ConfirmScreenHandler(int syncId, PlayerInventory playerInventory, Consumer<Boolean> cons) {
super(syncId, playerInventory, 1); super(syncId, playerInventory, 1, null);
this.cons = cons; this.cons = cons;
} }
@ -42,7 +42,7 @@ public class ConfirmScreenHandler extends ServerOnlyScreenHandler {
@Override @Override
protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData) { protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object additionalData) {
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
switch (i) { switch (i) {
case 3: case 3:

View File

@ -22,14 +22,25 @@ import net.minecraft.util.Formatting;
import java.util.List; import java.util.List;
public class GroupPlayerScreenHandler extends ServerOnlyScreenHandler { public class GroupPlayerScreenHandler extends ServerOnlyScreenHandler<ClaimGroup> {
private final Claim claim; private final Claim claim;
private final String group; private final String group;
private boolean removeMode; private boolean removeMode;
private GroupPlayerScreenHandler(int syncId, PlayerInventory playerInventory, Claim claim, String group) { private GroupPlayerScreenHandler(int syncId, PlayerInventory playerInventory, Claim claim, String group) {
super(syncId, playerInventory, 6, claim, group); super(syncId, playerInventory, 6, new ClaimGroup() {
@Override
public Claim getClaim() {
return claim;
}
@Override
public String getGroup() {
return group;
}
});
this.claim = claim; this.claim = claim;
this.group = group; this.group = group;
} }
@ -50,11 +61,9 @@ public class GroupPlayerScreenHandler extends ServerOnlyScreenHandler {
} }
@Override @Override
protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData) { protected void fillInventoryWith(PlayerEntity player, Inventory inv, ClaimGroup additionalData) {
if (additionalData == null || additionalData.length < 2) Claim claim = additionalData.getClaim();
return; List<String> players = claim.playersFromGroup(player.getServer(), additionalData.getGroup());
Claim claim = (Claim) additionalData[0];
List<String> players = claim.playersFromGroup(player.getServer(), (String) additionalData[1]);
for (int i = 0; i < 54; i++) { for (int i = 0; i < 54; i++) {
if (i == 0) { if (i == 0) {
ItemStack close = new ItemStack(Items.TNT); ItemStack close = new ItemStack(Items.TNT);

View File

@ -18,7 +18,7 @@ import net.minecraft.util.Formatting;
import java.util.List; import java.util.List;
public class GroupScreenHandler extends ServerOnlyScreenHandler { public class GroupScreenHandler extends ServerOnlyScreenHandler<Claim> {
private final Claim claim; private final Claim claim;
@ -45,10 +45,7 @@ public class GroupScreenHandler extends ServerOnlyScreenHandler {
} }
@Override @Override
protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData) { protected void fillInventoryWith(PlayerEntity player, Inventory inv, Claim claim) {
if (additionalData == null)
return;
Claim claim = (Claim) additionalData[0];
for (int i = 0; i < 54; i++) { for (int i = 0; i < 54; i++) {
if (i == 0) { if (i == 0) {
ItemStack close = new ItemStack(Items.TNT); ItemStack close = new ItemStack(Items.TNT);

View File

@ -21,14 +21,24 @@ import net.minecraft.util.Formatting;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class PermissionScreenHandler extends ServerOnlyScreenHandler { public class PermissionScreenHandler extends ServerOnlyScreenHandler<ClaimGroup> {
private final Claim claim; private final Claim claim;
private final String group; private final String group;
private int page; private int page;
private PermissionScreenHandler(int syncId, PlayerInventory playerInventory, Claim claim, String group, int page) { private PermissionScreenHandler(int syncId, PlayerInventory playerInventory, Claim claim, String group) {
super(syncId, playerInventory, 6, claim, group, page); super(syncId, playerInventory, 6, new ClaimGroup() {
@Override
public Claim getClaim() {
return claim;
}
@Override
public String getGroup() {
return group;
}
});
this.claim = claim; this.claim = claim;
this.group = group; this.group = group;
this.page = page; this.page = page;
@ -38,7 +48,7 @@ public class PermissionScreenHandler extends ServerOnlyScreenHandler {
NamedScreenHandlerFactory fac = new NamedScreenHandlerFactory() { NamedScreenHandlerFactory fac = new NamedScreenHandlerFactory() {
@Override @Override
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
return new PermissionScreenHandler(syncId, inv, claim, group, 0); return new PermissionScreenHandler(syncId, inv, claim, group);
} }
@Override @Override
@ -49,28 +59,13 @@ public class PermissionScreenHandler extends ServerOnlyScreenHandler {
player.openHandledScreen(fac); player.openHandledScreen(fac);
} }
private static void openClaimMenu(PlayerEntity player, Claim claim, String group, int page) {
NamedScreenHandlerFactory fac = new NamedScreenHandlerFactory() {
@Override @Override
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { protected void fillInventoryWith(PlayerEntity player, Inventory inv, ClaimGroup additionalData) {
return new PermissionScreenHandler(syncId, inv, claim, group, page);
}
@Override
public Text getDisplayName() {
return PermHelper.simpleColoredText(group == null ? ConfigHandler.lang.screenGlobalPerms : String.format(ConfigHandler.lang.screenGroupPerms, group));
}
};
player.openHandledScreen(fac);
}
@Override
protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData) {
List<ClaimPermission> perms = new ArrayList<>(PermissionRegistry.getPerms()); List<ClaimPermission> perms = new ArrayList<>(PermissionRegistry.getPerms());
if (this.group != null) if (this.group != null)
perms.removeAll(PermissionRegistry.globalPerms()); perms.removeAll(PermissionRegistry.globalPerms());
for (int i = 0; i < 54; i++) { for (int i = 0; i < 54; i++) {
int page = (int) additionalData[2]; int page = 0;
if (i == 0) { if (i == 0) {
ItemStack close = new ItemStack(Items.TNT); ItemStack close = new ItemStack(Items.TNT);
close.setCustomName(ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenBack, Formatting.DARK_RED)); close.setCustomName(ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenBack, Formatting.DARK_RED));
@ -89,7 +84,7 @@ public class PermissionScreenHandler extends ServerOnlyScreenHandler {
int row = i / 9 - 1; int row = i / 9 - 1;
int id = (i % 9) + row * 7 - 1 + page * 28; int id = (i % 9) + row * 7 - 1 + page * 28;
if (id < perms.size()) if (id < perms.size())
inv.setStack(i, ServerScreenHelper.fromPermission((Claim) additionalData[0], perms.get(id), additionalData[1] == null ? null : additionalData[1].toString())); inv.setStack(i, ServerScreenHelper.fromPermission(additionalData.getClaim(), perms.get(id), additionalData.getGroup() == null ? null : additionalData.getGroup()));
} }
} }
} }

View File

@ -20,12 +20,12 @@ import net.minecraft.util.Formatting;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class PersonalGroupScreenHandler extends ServerOnlyScreenHandler { public class PersonalGroupScreenHandler extends ServerOnlyScreenHandler<Object> {
private boolean removeMode; private boolean removeMode;
private PersonalGroupScreenHandler(int syncId, PlayerInventory playerInventory) { private PersonalGroupScreenHandler(int syncId, PlayerInventory playerInventory) {
super(syncId, playerInventory, 6); super(syncId, playerInventory, 6, null);
} }
public static void openGroupMenu(PlayerEntity player) { public static void openGroupMenu(PlayerEntity player) {
@ -44,7 +44,7 @@ public class PersonalGroupScreenHandler extends ServerOnlyScreenHandler {
} }
@Override @Override
protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData) { protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object additionalData) {
if (!(player instanceof ServerPlayerEntity)) if (!(player instanceof ServerPlayerEntity))
return; return;
for (int i = 0; i < 54; i++) { for (int i = 0; i < 54; i++) {

View File

@ -23,14 +23,14 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class PersonalPermissionScreenHandler extends ServerOnlyScreenHandler { public class PersonalPermissionScreenHandler extends ServerOnlyScreenHandler<String> {
private final String group; private final String group;
private int page; private int page;
private final PlayerEntity player; private final PlayerEntity player;
private PersonalPermissionScreenHandler(int syncId, PlayerInventory playerInventory, String group, int page) { private PersonalPermissionScreenHandler(int syncId, PlayerInventory playerInventory, String group) {
super(syncId, playerInventory, 6, group, page); super(syncId, playerInventory, 6, group);
this.group = group; this.group = group;
this.page = page; this.page = page;
this.player = playerInventory.player; this.player = playerInventory.player;
@ -40,7 +40,7 @@ public class PersonalPermissionScreenHandler extends ServerOnlyScreenHandler {
NamedScreenHandlerFactory fac = new NamedScreenHandlerFactory() { NamedScreenHandlerFactory fac = new NamedScreenHandlerFactory() {
@Override @Override
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
return new PersonalPermissionScreenHandler(syncId, inv, group, 0); return new PersonalPermissionScreenHandler(syncId, inv, group);
} }
@Override @Override
@ -52,14 +52,14 @@ public class PersonalPermissionScreenHandler extends ServerOnlyScreenHandler {
} }
@Override @Override
protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData) { protected void fillInventoryWith(PlayerEntity player, Inventory inv, String group) {
if (!(player instanceof ServerPlayerEntity)) if (!(player instanceof ServerPlayerEntity))
return; return;
List<ClaimPermission> perms = new ArrayList<>(PermissionRegistry.getPerms()); List<ClaimPermission> perms = new ArrayList<>(PermissionRegistry.getPerms());
if (this.group != null) if (this.group != null)
perms.removeAll(PermissionRegistry.globalPerms()); perms.removeAll(PermissionRegistry.globalPerms());
for (int i = 0; i < 54; i++) { for (int i = 0; i < 54; i++) {
int page = (int) additionalData[1]; int page = 0;
if (i == 0) { if (i == 0) {
ItemStack close = new ItemStack(Items.TNT); ItemStack close = new ItemStack(Items.TNT);
close.setCustomName(ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenBack, Formatting.DARK_RED)); close.setCustomName(ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenBack, Formatting.DARK_RED));
@ -78,7 +78,7 @@ public class PersonalPermissionScreenHandler extends ServerOnlyScreenHandler {
int row = i / 9 - 1; int row = i / 9 - 1;
int id = (i % 9) + row * 7 - 1 + page * 28; int id = (i % 9) + row * 7 - 1 + page * 28;
if (id < perms.size()) if (id < perms.size())
inv.setStack(i, ServerScreenHelper.getFromPersonal((ServerPlayerEntity) player, perms.get(id), additionalData[0] == null ? null : additionalData[0].toString())); inv.setStack(i, ServerScreenHelper.getFromPersonal((ServerPlayerEntity) player, perms.get(id), group == null ? null : group));
} }
} }
} }

View File

@ -27,7 +27,7 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class PotionEditScreenHandler extends ServerOnlyScreenHandler { public class PotionEditScreenHandler extends ServerOnlyScreenHandler<Claim> {
private final Claim claim; private final Claim claim;
@ -54,10 +54,7 @@ public class PotionEditScreenHandler extends ServerOnlyScreenHandler {
} }
@Override @Override
protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData) { protected void fillInventoryWith(PlayerEntity player, Inventory inv, Claim claim) {
if (additionalData == null)
return;
Claim claim = (Claim) additionalData[0];
Map<StatusEffect, Integer> potions = claim.getPotions(); Map<StatusEffect, Integer> potions = claim.getPotions();
List<StatusEffect> key = Lists.newArrayList(potions.keySet()); List<StatusEffect> key = Lists.newArrayList(potions.keySet());
key.sort(Comparator.comparing(CrossPlatformStuff::stringFromEffect)); key.sort(Comparator.comparing(CrossPlatformStuff::stringFromEffect));

View File

@ -16,12 +16,12 @@ import net.minecraft.server.network.ServerPlayerEntity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public abstract class ServerOnlyScreenHandler extends ScreenHandler { public abstract class ServerOnlyScreenHandler<T> extends ScreenHandler {
private final Inventory inventory; private final Inventory inventory;
private final List<ScreenHandlerListener> listeners = new ArrayList<>(); private final List<ScreenHandlerListener> listeners = new ArrayList<>();
protected ServerOnlyScreenHandler(int syncId, PlayerInventory playerInventory, int rows, Object... additionalData) { protected ServerOnlyScreenHandler(int syncId, PlayerInventory playerInventory, int rows, T additionalData) {
super(fromRows(rows), syncId); super(fromRows(rows), syncId);
int i = (rows - 4) * 18; int i = (rows - 4) * 18;
this.inventory = new SimpleInventory(rows * 9); this.inventory = new SimpleInventory(rows * 9);
@ -61,7 +61,7 @@ public abstract class ServerOnlyScreenHandler extends ScreenHandler {
return ScreenHandlerType.GENERIC_9X1; return ScreenHandlerType.GENERIC_9X1;
} }
protected abstract void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData); protected abstract void fillInventoryWith(PlayerEntity player, Inventory inv, T additionalData);
@Override @Override
public boolean canUse(PlayerEntity player) { public boolean canUse(PlayerEntity player) {