Skip to content

Commit 50c3f1d

Browse files
committed
try improve ender fluid security
1 parent 4bec1cd commit 50c3f1d

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;
@@ -96,6 +99,11 @@ protected final UUID getOwner() {
9699
return isPrivate ? playerUUID : null;
97100
}
98101

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

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

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

152-
var entrySelectorSH = syncManager.panel("entry_selector", entrySelector(getType()), true);
169+
IPanelHandler entrySelectorSH = syncManager.panel("entry_selector", entrySelector(getType(), uuid), true);
153170

154171
return Flow.column().coverChildrenHeight().top(24)
155172
.margin(7, 0).widthRel(1f)
156173
.child(Flow.row().marginBottom(2)
157174
.coverChildrenHeight()
158-
.child(createPrivateButton())
175+
.child(createPrivateButton(uuid))
159176
.child(createColorIcon())
160177
.child(new TextFieldWidget()
161178
// todo remove 'keepScrollBarInArea' when this is fixed in mui2
@@ -181,7 +198,7 @@ protected Flow createWidgets(GuiData data, PanelSyncManager syncManager) {
181198
}
182199
return true;
183200
})))
184-
.child(createIoRow());
201+
.child(createIoRow(uuid));
185202
}
186203

187204
protected abstract IWidget createEntrySlot();
@@ -196,29 +213,39 @@ protected IWidget createColorIcon() {
196213
.marginRight(2);
197214
}
198215

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

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

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

266295
@Override
@@ -285,11 +314,11 @@ public void writeToNBT(@NotNull NBTTagCompound nbt) {
285314
nbt.setInteger("Frequency", activeEntry.getColor());
286315
}
287316

288-
protected PanelSyncHandler.IPanelBuilder entrySelector(EntryTypes<T> type) {
317+
protected PanelSyncHandler.IPanelBuilder entrySelector(EntryTypes<T> type, UUID uuid) {
289318
return (syncManager, syncHandler) -> {
290319
List<IWidget> rows = new ArrayList<>();
291320
for (String name : VirtualEnderRegistry.getEntryNames(getOwner(), type)) {
292-
rows.add(createRow(name, syncManager, type));
321+
rows.add(createRow(name, syncManager, type, uuid));
293322
}
294323
return GTGuis.createPopupPanel("entry_selector", 168, 112, true)
295324
.child(IKey.lang("cover.generic.ender.known_channels")
@@ -299,7 +328,6 @@ protected PanelSyncHandler.IPanelBuilder entrySelector(EntryTypes<T> type) {
299328
.left(4))
300329
.child(new ListWidget<>()
301330
.children(rows)
302-
// .builder(names, name -> createRow(name, syncManager, type))
303331
.background(GTGuiTextures.DISPLAY.asIcon()
304332
.width(168 - 8)
305333
.height(112 - 20))
@@ -342,13 +370,15 @@ protected PanelSyncHandler.IPanelBuilder entryDescription(String key, T entry) {
342370
};
343371
}
344372

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

354384
return Flow.row()
@@ -364,8 +394,13 @@ protected IWidget createRow(final String name, final PanelSyncManager syncManage
364394
.size(16)
365395
.background(GTGuiTextures.SLOT.asIcon().size(18))
366396
.top(1))
367-
.child(new InteractableText<>(entry, this::updateColor)
368-
.tooltipAutoUpdate(true)
397+
.child(new InteractableText<>(entry, str -> {
398+
if (canAccess(uuid)) {
399+
updateColor(str);
400+
return true;
401+
}
402+
return false;
403+
}).tooltipAutoUpdate(true)
369404
.tooltipBuilder(tooltip -> {
370405
String desc = entry.getDescription();
371406
if (!desc.isEmpty()) tooltip.add(desc);
@@ -378,6 +413,8 @@ protected IWidget createRow(final String name, final PanelSyncManager syncManage
378413
.overlay(GuiTextures.GEAR)
379414
.addTooltipLine(IKey.lang("cover.generic.ender.set_description.tooltip"))
380415
.onMousePressed(i -> {
416+
if (!canAccess(uuid)) return false;
417+
381418
// open entry settings
382419
if (panelHandler.isPanelOpen()) {
383420
panelHandler.closePanel();
@@ -392,6 +429,8 @@ protected IWidget createRow(final String name, final PanelSyncManager syncManage
392429
.setEnabledIf(w -> !Objects.equals(entry.getColor(), activeEntry.getColor()))
393430
.addTooltipLine(IKey.lang("cover.generic.ender.delete_entry"))
394431
.onMousePressed(i -> {
432+
if (!canAccess(uuid)) return false;
433+
395434
// todo option to force delete, maybe as a popup?
396435
deleteEntry(getOwner(), name);
397436
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<InteractableText<T>> 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)