Skip to content

Commit 8a44a79

Browse files
committed
try improve ender fluid security
1 parent bfed216 commit 8a44a79

File tree

4 files changed

+97
-42
lines changed

4 files changed

+97
-42
lines changed

src/main/java/gregtech/api/util/virtualregistry/VirtualRegistryMap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.jetbrains.annotations.NotNull;
77
import org.jetbrains.annotations.Nullable;
88

9+
import java.util.Collections;
910
import java.util.HashMap;
1011
import java.util.Map;
1112
import java.util.Set;
@@ -49,7 +50,7 @@ public void clear() {
4950
}
5051

5152
public Set<String> getEntryNames(EntryTypes<?> type) {
52-
return registryMap.get(type).keySet();
53+
return registryMap.getOrDefault(type, Collections.emptyMap()).keySet();
5354
}
5455

5556
@Override

src/main/java/gregtech/common/covers/ender/CoverAbstractEnderLink.java

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import gregtech.api.util.virtualregistry.VirtualEnderRegistry;
1313
import gregtech.api.util.virtualregistry.VirtualEntry;
1414
import gregtech.common.mui.widget.InteractableText;
15+
import gregtech.integration.ftb.utility.FTBTeamHelper;
1516

1617
import net.minecraft.entity.player.EntityPlayer;
1718
import net.minecraft.entity.player.EntityPlayerMP;
@@ -24,7 +25,9 @@
2425
import net.minecraft.util.ITickable;
2526

2627
import codechicken.lib.raytracer.CuboidRayTraceResult;
28+
import com.cleanroommc.modularui.api.IPanelHandler;
2729
import com.cleanroommc.modularui.api.drawable.IKey;
30+
import com.cleanroommc.modularui.api.value.IBoolValue;
2831
import com.cleanroommc.modularui.api.widget.IWidget;
2932
import com.cleanroommc.modularui.drawable.DynamicDrawable;
3033
import com.cleanroommc.modularui.drawable.GuiTextures;
@@ -95,6 +98,11 @@ protected final UUID getOwner() {
9598
return isPrivate ? playerUUID : null;
9699
}
97100

101+
protected boolean canAccess(UUID other) {
102+
return this.playerUUID == null || this.playerUUID.equals(other) ||
103+
FTBTeamHelper.isSameTeam(this.playerUUID, other);
104+
}
105+
98106
@Override
99107
public void readCustomData(int discriminator, @NotNull PacketBuffer buf) {
100108
super.readCustomData(discriminator, buf);
@@ -136,25 +144,34 @@ public boolean usesMui2() {
136144

137145
@Override
138146
public ModularPanel buildUI(SidedPosGuiData guiData, PanelSyncManager guiSyncManager) {
139-
var panel = GTGuis.createPanel(this, 176, 192);
140-
141-
this.playerUUID = guiData.getPlayer().getUniqueID();
142-
143-
return panel.child(CoverWithUI.createTitleRow(getPickItem()))
147+
if (!isPrivate()) {
148+
// if we're public, we update the player uuid
149+
// so when set to private, it'll use the latest player uuid
150+
this.playerUUID = guiData.getPlayer().getUniqueID();
151+
}
152+
return GTGuis.createPanel(this, 176, 192)
153+
.child(CoverWithUI.createTitleRow(getPickItem()))
144154
.child(createWidgets(guiData, guiSyncManager))
145155
.bindPlayerInventory();
146156
}
147157

148158
protected Flow createWidgets(GuiData data, PanelSyncManager syncManager) {
149-
var name = new StringSyncValue(this::getColorStr, this::updateColor);
159+
StringSyncValue name;
160+
UUID uuid = data.getPlayer().getUniqueID();
161+
162+
if (canAccess(uuid)) {
163+
name = new StringSyncValue(this::getColorStr, this::updateColor);
164+
} else {
165+
name = new StringSyncValue(this::getColorStr);
166+
}
150167

151-
var entrySelectorSH = syncManager.panel("entry_selector", entrySelector(getType()), true);
168+
IPanelHandler entrySelectorSH = syncManager.panel("entry_selector", entrySelector(getType(), uuid), true);
152169

153170
return Flow.column().coverChildrenHeight().top(24)
154171
.margin(7, 0).widthRel(1f)
155172
.child(Flow.row().marginBottom(2)
156173
.coverChildrenHeight()
157-
.child(createPrivateButton())
174+
.child(createPrivateButton(uuid))
158175
.child(createColorIcon())
159176
.child(new TextFieldWidget()
160177
// todo remove 'keepScrollBarInArea' when this is fixed in mui2
@@ -180,7 +197,7 @@ protected Flow createWidgets(GuiData data, PanelSyncManager syncManager) {
180197
}
181198
return true;
182199
})))
183-
.child(createIoRow());
200+
.child(createIoRow(uuid));
184201
}
185202

186203
protected abstract IWidget createEntrySlot();
@@ -195,29 +212,39 @@ protected IWidget createColorIcon() {
195212
.marginRight(2);
196213
}
197214

198-
protected IWidget createPrivateButton() {
215+
protected IWidget createPrivateButton(UUID uniqueID) {
216+
BooleanSyncValue syncValue;
217+
if (canAccess(uniqueID))
218+
syncValue = new BooleanSyncValue(this::isPrivate, this::setPrivate);
219+
else {
220+
syncValue = new BooleanSyncValue(this::isPrivate);
221+
}
199222
return new ToggleButton()
200-
.value(new BooleanSyncValue(this::isPrivate, this::setPrivate))
201-
.tooltip(tooltip -> tooltip.setAutoUpdate(true))
223+
.value(syncValue)
202224
.background(GTGuiTextures.PRIVATE_MODE_BUTTON[0])
203225
.hoverBackground(GTGuiTextures.PRIVATE_MODE_BUTTON[0])
204226
.selectedBackground(GTGuiTextures.PRIVATE_MODE_BUTTON[1])
205227
.selectedHoverBackground(GTGuiTextures.PRIVATE_MODE_BUTTON[1])
206-
.tooltipBuilder(tooltip -> tooltip.addLine(IKey.lang(this.isPrivate ?
207-
"cover.ender_fluid_link.private.tooltip.enabled" :
208-
"cover.ender_fluid_link.private.tooltip.disabled")))
228+
.addTooltip(true, IKey.lang("cover.ender_fluid_link.private.tooltip.enabled"))
229+
.addTooltip(false, IKey.lang("cover.ender_fluid_link.private.tooltip.disabled"))
209230
.marginRight(2);
210231
}
211232

212-
protected IWidget createIoRow() {
233+
protected IWidget createIoRow(UUID uuid) {
234+
BooleanSyncValue syncValue;
235+
if (canAccess(uuid)) {
236+
syncValue = new BooleanSyncValue(this::isIoEnabled, this::setIoEnabled);
237+
} else {
238+
syncValue = new BooleanSyncValue(this::isIoEnabled);
239+
}
213240
return Flow.row().marginBottom(2)
214241
.coverChildrenHeight()
215242
.child(new ToggleButton()
216-
.value(new BooleanSyncValue(this::isIoEnabled, this::setIoEnabled))
217-
.overlay(IKey.lang(() -> this.ioEnabled ?
218-
"behaviour.soft_hammer.enabled" :
219-
"behaviour.soft_hammer.disabled")
220-
.color(Color.WHITE.darker(1)))
243+
.value(syncValue)
244+
.overlay(true, IKey.lang("behaviour.soft_hammer.enabled")
245+
.color(Color.WHITE.main))
246+
.overlay(false, IKey.lang("behaviour.soft_hammer.disabled")
247+
.color(Color.WHITE.main))
221248
.widthRel(0.6f)
222249
.left(0));
223250
}
@@ -253,13 +280,15 @@ private void setPrivate(boolean isPrivate) {
253280
@Override
254281
public void writeInitialSyncData(PacketBuffer packetBuffer) {
255282
packetBuffer.writeString(this.playerUUID == null ? "null" : this.playerUUID.toString());
283+
packetBuffer.writeBoolean(this.isPrivate);
256284
}
257285

258286
@Override
259287
public void readInitialSyncData(PacketBuffer packetBuffer) {
260288
// does client even need uuid info? just in case
261289
String uuidStr = packetBuffer.readString(36);
262290
this.playerUUID = uuidStr.equals("null") ? null : UUID.fromString(uuidStr);
291+
this.isPrivate = packetBuffer.readBoolean();
263292
}
264293

265294
@Override
@@ -284,11 +313,11 @@ public void writeToNBT(@NotNull NBTTagCompound nbt) {
284313
nbt.setInteger("Frequency", activeEntry.getColor());
285314
}
286315

287-
protected PanelSyncHandler.IPanelBuilder entrySelector(EntryTypes<T> type) {
316+
protected PanelSyncHandler.IPanelBuilder entrySelector(EntryTypes<T> type, UUID uuid) {
288317
return (syncManager, syncHandler) -> {
289318
List<IWidget> rows = new ArrayList<>();
290319
for (String name : VirtualEnderRegistry.getEntryNames(getOwner(), type)) {
291-
rows.add(createRow(name, syncManager, type));
320+
rows.add(createRow(name, syncManager, type, uuid));
292321
}
293322
return GTGuis.createPopupPanel("entry_selector", 168, 112, true)
294323
.child(IKey.lang("cover.generic.ender.known_channels")
@@ -298,7 +327,6 @@ protected PanelSyncHandler.IPanelBuilder entrySelector(EntryTypes<T> type) {
298327
.left(4))
299328
.child(new ListWidget<>()
300329
.children(rows)
301-
// .builder(names, name -> createRow(name, syncManager, type))
302330
.background(GTGuiTextures.DISPLAY.asIcon()
303331
.width(168 - 8)
304332
.height(112 - 20))
@@ -341,13 +369,15 @@ protected PanelSyncHandler.IPanelBuilder entryDescription(String key, T entry) {
341369
};
342370
}
343371

344-
protected IWidget createRow(final String name, final PanelSyncManager syncManager, final EntryTypes<T> type) {
345-
final T entry = VirtualEnderRegistry.getEntry(getOwner(), type, name);
346-
var key = String.format("entry#%s_description", entry.getColorStr());
347-
var syncKey = PanelSyncManager.makeSyncKey(key, isPrivate ? 1 : 0);
348-
final var panelHandler = (PanelSyncHandler) syncManager.panel(syncKey,
372+
protected IWidget createRow(String name, PanelSyncManager syncManager,
373+
EntryTypes<T> type, UUID uuid) {
374+
T entry = VirtualEnderRegistry.getEntry(getOwner(), type, name);
375+
String key = String.format("entry#%s_description", entry.getColorStr());
376+
String syncKey = PanelSyncManager.makeSyncKey(key, isPrivate ? 1 : 0);
377+
IPanelHandler panelHandler = syncManager.panel(syncKey,
349378
entryDescription(key, entry), true);
350-
final var syncHandler = new EnderCoverSyncHandler();
379+
380+
EnderCoverSyncHandler syncHandler = new EnderCoverSyncHandler();
351381
syncManager.syncValue(key + "_handler", syncHandler);
352382

353383
return Flow.row()
@@ -363,8 +393,13 @@ protected IWidget createRow(final String name, final PanelSyncManager syncManage
363393
.size(16)
364394
.background(GTGuiTextures.SLOT.asIcon().size(18))
365395
.top(1))
366-
.child(new InteractableText<>(entry, this::updateColor)
367-
.tooltip(tooltip -> tooltip.setAutoUpdate(true))
396+
.child(new InteractableText<>(entry, str -> {
397+
if (canAccess(uuid)) {
398+
updateColor(str);
399+
return true;
400+
}
401+
return false;
402+
}).tooltipAutoUpdate(true)
368403
.tooltipBuilder(tooltip -> {
369404
String desc = entry.getDescription();
370405
if (!desc.isEmpty()) tooltip.add(desc);
@@ -377,6 +412,8 @@ protected IWidget createRow(final String name, final PanelSyncManager syncManage
377412
.overlay(GuiTextures.GEAR)
378413
.addTooltipLine(IKey.lang("cover.generic.ender.set_description.tooltip"))
379414
.onMousePressed(i -> {
415+
if (!canAccess(uuid)) return false;
416+
380417
// open entry settings
381418
if (panelHandler.isPanelOpen()) {
382419
panelHandler.closePanel();
@@ -391,6 +428,8 @@ protected IWidget createRow(final String name, final PanelSyncManager syncManage
391428
.setEnabledIf(w -> !Objects.equals(entry.getColor(), activeEntry.getColor()))
392429
.addTooltipLine(IKey.lang("cover.generic.ender.delete_entry"))
393430
.onMousePressed(i -> {
431+
if (!canAccess(uuid)) return false;
432+
394433
// todo option to force delete, maybe as a popup?
395434
deleteEntry(getOwner(), name);
396435
syncHandler.syncToServer(1, buffer -> {

src/main/java/gregtech/common/mui/widget/InteractableText.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
import com.cleanroommc.modularui.widgets.TextWidget;
1414
import org.jetbrains.annotations.NotNull;
1515

16-
import java.util.function.Consumer;
16+
import java.util.function.Function;
1717

1818
public class InteractableText<T extends VirtualEntry> extends TextWidget implements Interactable {
1919

2020
private final T entry;
2121
private final EntryColorSH syncHandler;
2222

23-
public InteractableText(T entry, Consumer<String> setter) {
23+
public InteractableText(T entry, Function<String, Boolean> setter) {
2424
super(IKey.str(entry.getColorStr())
2525
.alignment(Alignment.CenterLeft)
2626
.color(Color.WHITE.darker(1)));
@@ -32,9 +32,10 @@ public InteractableText(T entry, Consumer<String> setter) {
3232
@NotNull
3333
@Override
3434
public Result onMousePressed(int mouseButton) {
35-
Interactable.playButtonClickSound();
36-
this.syncHandler.setColor(this.entry.getColorStr());
37-
this.syncHandler.syncToServer(1, buf -> NetworkUtils.writeStringSafe(buf, this.entry.getColorStr()));
35+
if (this.syncHandler.setColor(this.entry.getColorStr())) {
36+
Interactable.playButtonClickSound();
37+
this.syncHandler.syncToServer(1, buf -> NetworkUtils.writeStringSafe(buf, this.entry.getColorStr()));
38+
}
3839
return Result.SUCCESS;
3940
}
4041

@@ -45,14 +46,14 @@ public boolean isValidSyncHandler(SyncHandler syncHandler) {
4546

4647
private static class EntryColorSH extends SyncHandler {
4748

48-
private final Consumer<String> setter;
49+
private final Function<String, Boolean> setter;
4950

50-
private EntryColorSH(Consumer<String> setter) {
51+
private EntryColorSH(Function<String, Boolean> setter) {
5152
this.setter = setter;
5253
}
5354

54-
public void setColor(String c) {
55-
this.setter.accept(c);
55+
public boolean setColor(String c) {
56+
return this.setter.apply(c);
5657
}
5758

5859
@Override
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package gregtech.integration.ftb.utility;
2+
3+
import gregtech.api.util.Mods;
4+
5+
import com.feed_the_beast.ftblib.lib.data.FTBLibAPI;
6+
7+
import java.util.UUID;
8+
9+
public class FTBTeamHelper {
10+
11+
public static boolean isSameTeam(UUID first, UUID second) {
12+
return Mods.FTB_UTILITIES.isModLoaded() && FTBLibAPI.arePlayersInSameTeam(first, second);
13+
}
14+
}

0 commit comments

Comments
 (0)