Skip to content

Commit

Permalink
Add adventure mode access toggles
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiDragon committed Jul 16, 2023
1 parent 50cef6a commit 70c135a
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 67 deletions.
3 changes: 2 additions & 1 deletion changelog/2.0.2+1.20.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
* Updated nodeflow version now prevents this from happening
* Improved keyboard navigation
* Added controlify support
* Currently interacting with network controllers using a controller causes a crash due to controlify bug.
* Currently interacting with network controllers using a controller causes a crash due to controlify bug.
* Add adventure mode access toggles
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.mattidragon.advancednetworking.graph.NetworkControllerContext;
import io.github.mattidragon.advancednetworking.misc.RequestInterfacesPacket;
import io.github.mattidragon.advancednetworking.misc.ScreenPosSyncPacket;
import io.github.mattidragon.advancednetworking.misc.SetAdventureModeAccessPacket;
import io.github.mattidragon.advancednetworking.misc.UpdateInterfacePacket;
import io.github.mattidragon.advancednetworking.network.NetworkRegistry;
import io.github.mattidragon.advancednetworking.registry.ModBlocks;
Expand Down Expand Up @@ -56,6 +57,7 @@ public void onInitialize() {
ScreenPosSyncPacket.register();
UpdateInterfacePacket.register();
RequestInterfacesPacket.register();
SetAdventureModeAccessPacket.register();
NetworkControllerContext.register();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.mattidragon.advancednetworking.block;

public interface AdventureModeAccessBlockEntity {
boolean isAdventureModeAccessAllowed();
void setAdventureModeAccessAllowed(boolean allowed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ public static void changeMode(World world, BlockState state, BlockPos pos, Direc

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!(world.getBlockEntity(pos) instanceof CableBlockEntity cable)) return ActionResult.PASS;
if (!player.getAbilities().allowModifyWorld && !cable.isAdventureModeAccessAllowed()) return ActionResult.PASS;

var direction = calcHitDirection(hit.getPos().subtract(Vec3d.of(pos)));
var property = FACING_PROPERTIES.get(direction);

Expand All @@ -254,8 +257,8 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
return ActionResult.SUCCESS;
}
if (hand == Hand.MAIN_HAND && player.isSneaking()) {
if (world.isClient && world.getBlockEntity(pos) instanceof CableBlockEntity cable) {
openConfig(pos.toImmutable(), direction, (side) -> InterfaceType.ofConnectionType(world.getBlockState(pos).get(FACING_PROPERTIES.get(side))), cable::getName);
if (world.isClient) {
openConfig(pos.toImmutable(), direction, (side) -> InterfaceType.ofConnectionType(world.getBlockState(pos).get(FACING_PROPERTIES.get(side))), cable::getName, cable.isAdventureModeAccessAllowed());
}
return ActionResult.SUCCESS;
}
Expand All @@ -264,8 +267,8 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
}

@Environment(EnvType.CLIENT)
private void openConfig(BlockPos pos, Direction direction, Function<Direction, InterfaceType> typeSupplier, Function<Direction, String> nameSupplier) {
MinecraftClient.getInstance().setScreen(new CableConfigScreen(pos, direction, typeSupplier, nameSupplier));
private void openConfig(BlockPos pos, Direction direction, Function<Direction, InterfaceType> typeSupplier, Function<Direction, String> nameSupplier, boolean allowAdventureModeAccess) {
MinecraftClient.getInstance().setScreen(new CableConfigScreen(pos, direction, typeSupplier, nameSupplier, allowAdventureModeAccess));
}

private Direction calcHitDirection(Vec3d pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

import java.util.Arrays;

public class CableBlockEntity extends BlockEntity {
public class CableBlockEntity extends BlockEntity implements AdventureModeAccessBlockEntity {
private final int[] power = new int[6];
private final String[] names = new String[6];
private boolean allowAdventureModeAccess = false;

public CableBlockEntity(BlockPos pos, BlockState state) {
super(ModBlocks.CABLE_BLOCK_ENTITY, pos, state);
Expand Down Expand Up @@ -64,6 +65,7 @@ public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
var power = nbt.getIntArray("power");
System.arraycopy(power, 0, this.power, 0, Math.min(power.length, 6));
allowAdventureModeAccess = nbt.getBoolean("allowAdventureModeAccess");

var names = nbt.getList("names", NbtElement.STRING_TYPE);
for (int i = 0; i < Math.min(names.size(), 6); i++) {
Expand All @@ -75,11 +77,20 @@ public void readNbt(NbtCompound nbt) {
protected void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
nbt.putIntArray("power", power);
nbt.putBoolean("allowAdventureModeAccess", allowAdventureModeAccess);

var names = new NbtList();
for (var name : this.names) {
names.add(NbtString.of(name));
}
nbt.put("names", names);
}

public boolean isAdventureModeAccessAllowed() {
return allowAdventureModeAccess;
}

public void setAdventureModeAccessAllowed(boolean allowed) {
this.allowAdventureModeAccess = allowed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ public BlockRenderType getRenderType(BlockState state) {

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!world.isClient) {
NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
if (!(world.getBlockEntity(pos) instanceof ControllerBlockEntity controller)) return ActionResult.PASS;
if (!controller.isAdventureModeAccessAllowed() && !player.getAbilities().allowModifyWorld) return ActionResult.PASS;
if (world.isClient) return ActionResult.SUCCESS;
NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);

if (screenHandlerFactory != null) {
player.openHandledScreen(screenHandlerFactory);
}
if (screenHandlerFactory != null) {
player.openHandledScreen(screenHandlerFactory);
}
return ActionResult.SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
import java.util.Optional;
import java.util.function.Function;

public class ControllerBlockEntity extends GraphProvidingBlockEntity {
public class ControllerBlockEntity extends GraphProvidingBlockEntity implements AdventureModeAccessBlockEntity {
private Graph graph = new Graph(AdvancedNetworking.ENVIRONMENT);
private boolean allowAdventureModeAccess = false;

public final PathEnvironment<Storage<ItemVariant>, ItemTransformer> itemEnvironment = new PathEnvironment<>();
public final PathEnvironment<Storage<FluidVariant>, FluidTransformer> fluidEnvironment = new PathEnvironment<>();
Expand All @@ -67,6 +68,7 @@ public Text getDisplayName() {
@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
allowAdventureModeAccess = nbt.getBoolean("allowAdventureModeAccess");
viewX = nbt.getDouble("viewX");
viewY = nbt.getDouble("viewY");
zoom = nbt.getInt("zoom");
Expand All @@ -81,6 +83,7 @@ public void readNbt(NbtCompound nbt) {
@Override
protected void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
nbt.putBoolean("allowAdventureModeAccess", allowAdventureModeAccess);
nbt.putDouble("viewX", viewX);
nbt.putDouble("viewY", viewY);
nbt.putInt("zoom", zoom);
Expand Down Expand Up @@ -146,6 +149,7 @@ public void writeScreenOpeningData(ServerPlayerEntity player, PacketByteBuf buf)
buf.writeDouble(viewX);
buf.writeDouble(viewY);
buf.writeCollection(errors, PacketByteBuf::writeText);
buf.writeBoolean(allowAdventureModeAccess);
}

@Override
Expand All @@ -162,4 +166,12 @@ public void setGraph(Graph graph, World world, BlockPos pos) {
public Graph getGraph(World world, BlockPos pos) {
return graph;
}

public boolean isAdventureModeAccessAllowed() {
return allowAdventureModeAccess;
}

public void setAdventureModeAccessAllowed(boolean allowed) {
this.allowAdventureModeAccess = allowed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import io.github.mattidragon.advancednetworking.block.CableBlock;
import io.github.mattidragon.advancednetworking.misc.InterfaceType;
import io.github.mattidragon.advancednetworking.misc.SetAdventureModeAccessPacket;
import io.github.mattidragon.advancednetworking.misc.UpdateInterfacePacket;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
Expand All @@ -21,13 +23,15 @@ public class CableConfigScreen extends Screen {
private Direction side;
private InterfaceType type;
private String name;
private boolean adventureModeAccessAllowed;

public CableConfigScreen(BlockPos pos, Direction side, Function<Direction, InterfaceType> typeSupplier, Function<Direction, String> nameSupplier) {
public CableConfigScreen(BlockPos pos, Direction side, Function<Direction, InterfaceType> typeSupplier, Function<Direction, String> nameSupplier, boolean adventureModeAccessAllowed) {
super(Text.translatable("screen.advanced_networking.cable_config"));
this.pos = pos;
this.typeSupplier = typeSupplier;
this.nameSupplier = nameSupplier;
this.side = side;
this.adventureModeAccessAllowed = adventureModeAccessAllowed;
this.type = typeSupplier.apply(side);
this.name = nameSupplier.apply(side);
}
Expand All @@ -50,7 +54,7 @@ protected void init() {
button1.active = false;
buttons[side.getId()].active = true;

UpdateInterfacePacket.send(pos, side, type, name);
ClientPlayNetworking.send(new UpdateInterfacePacket(pos, side, type, name));
side = direction;
type = typeSupplier.apply(side);
name = nameSupplier.apply(side);
Expand All @@ -61,6 +65,15 @@ protected void init() {
buttons[i] = button;
}
buttons[side.getId()].active = false;

if (client != null && client.player != null && client.player.isCreativeLevelTwoOp()) {
addDrawableChild(CyclingButtonWidget.onOffBuilder()
.initially(adventureModeAccessAllowed)
.build(calcLeftX(), 170, 150, 20, Text.translatable("screen.advanced_networking.adventure_mode_access"), (button, value) -> {
adventureModeAccessAllowed = value;
ClientPlayNetworking.send(new SetAdventureModeAccessPacket(pos, adventureModeAccessAllowed));
}));
}
}

@Override
Expand All @@ -71,7 +84,7 @@ public boolean shouldPause() {
@Override
public void close() {
super.close();
UpdateInterfacePacket.send(pos, side, type, name);
ClientPlayNetworking.send(new UpdateInterfacePacket(pos, side, type, name));
}

private int calcLeftX() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import io.github.mattidragon.advancednetworking.misc.ScreenPosSyncPacket;
import io.github.mattidragon.advancednetworking.screen.ControllerScreenHandler;
import io.github.mattidragon.nodeflow.ui.screen.HandledEditorScreen;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.widget.CyclingButtonWidget;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.MutableText;
Expand All @@ -29,12 +31,22 @@ protected void init() {
area.setViewX(handler.viewX);
area.setViewY(handler.viewY);
area.setZoom(handler.zoom);

if (client != null && client.player != null && client.player.isCreativeLevelTwoOp()) {
addDrawableChild(CyclingButtonWidget.onOffBuilder()
.initially(handler.adventureModeAccessAllowed)
.build(GRID_OFFSET, GRID_OFFSET + getBoxHeight() + BORDER_SIZE, 150, 20, Text.translatable("screen.advanced_networking.adventure_mode_access"), (button, value) -> {
if (client.interactionManager == null) return;
handler.adventureModeAccessAllowed = !handler.adventureModeAccessAllowed;
client.interactionManager.clickButton(handler.syncId, 0);
}));
}
}

@Override
public void syncGraph() {
super.syncGraph();
ScreenPosSyncPacket.send(getScreenHandler().syncId, area.getViewX(), area.getViewY(), area.getZoom());
ClientPlayNetworking.send(new ScreenPosSyncPacket(getScreenHandler().syncId, area.getViewX(), area.getViewY(), area.getZoom()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
package io.github.mattidragon.advancednetworking.misc;

import io.github.mattidragon.advancednetworking.screen.ControllerScreenHandler;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

import static io.github.mattidragon.advancednetworking.AdvancedNetworking.id;

public class ScreenPosSyncPacket {
public record ScreenPosSyncPacket(int syncId, double x, double y, int zoom) implements FabricPacket {
private static final Identifier ID = id("pos_sync");
private static final PacketType<ScreenPosSyncPacket> TYPE = PacketType.create(ID, ScreenPosSyncPacket::new);

public static void register() {
ServerPlayNetworking.registerGlobalReceiver(ID, ((server, player, handler, buf, responseSender) -> {
var x = buf.readDouble();
var y = buf.readDouble();
var zoom = buf.readInt();
var syncId = buf.readByte();
public ScreenPosSyncPacket(PacketByteBuf buf) {
this(buf.readByte(), buf.readDouble(), buf.readDouble(), buf.readInt());
}

server.execute(() -> {
if (player.currentScreenHandler.syncId == syncId && player.currentScreenHandler instanceof ControllerScreenHandler networking) {
networking.viewX = x;
networking.viewY = y;
networking.zoom = zoom;
}
});
}));
public static void register() {
ServerPlayNetworking.registerGlobalReceiver(TYPE, (packet, player, responseSender) -> {
if (player.currentScreenHandler.syncId == packet.syncId && player.currentScreenHandler instanceof ControllerScreenHandler networking) {
networking.viewX = packet.x;
networking.viewY = packet.y;
networking.zoom = packet.zoom;
}
});
}

public static void send(int syncId, double x, double y, int zoom) {
var buf = PacketByteBufs.create();
@Override
public void write(PacketByteBuf buf) {
buf.writeByte(syncId);
buf.writeDouble(x);
buf.writeDouble(y);
buf.writeInt(zoom);
buf.writeByte(syncId);
}

ClientPlayNetworking.send(ID, buf);
@Override
public PacketType<?> getType() {
return TYPE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.mattidragon.advancednetworking.misc;

import io.github.mattidragon.advancednetworking.block.AdventureModeAccessBlockEntity;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;

import static io.github.mattidragon.advancednetworking.AdvancedNetworking.id;

public record SetAdventureModeAccessPacket(BlockPos pos, boolean allowAccess) implements FabricPacket {
private static final Identifier ID = id("set_adventure_mode_access");
private static final PacketType<SetAdventureModeAccessPacket> TYPE = PacketType.create(ID, SetAdventureModeAccessPacket::new);

public SetAdventureModeAccessPacket(PacketByteBuf buf) {
this(buf.readBlockPos(), buf.readBoolean());
}

public static void register() {
ServerPlayNetworking.registerGlobalReceiver(TYPE, (packet, player, responseSender) -> {
if (!player.isCreativeLevelTwoOp()) return;
var world = player.getWorld();
if (world.getBlockEntity(packet.pos) instanceof AdventureModeAccessBlockEntity blockEntity) {
blockEntity.setAdventureModeAccessAllowed(packet.allowAccess);
}
});
}

@Override
public void write(PacketByteBuf buf) {
buf.writeBlockPos(pos);
buf.writeBoolean(allowAccess);
}

@Override
public PacketType<?> getType() {
return TYPE;
}
}
Loading

0 comments on commit 70c135a

Please sign in to comment.