long overdue redo of s2c networking, fix a crash from the laser pointer

This commit is contained in:
GiantLuigi4 2023-06-05 18:23:05 -04:00
parent 5ce9e4574d
commit 9cfbc07b44
16 changed files with 315 additions and 302 deletions

View File

@ -76,7 +76,7 @@ import net.montoyo.wd.item.ItemMinePad2;
import net.montoyo.wd.item.WDItem;
import net.montoyo.wd.miniserv.client.Client;
import net.montoyo.wd.net.WDNetworkRegistry;
import net.montoyo.wd.net.server_bound.C2SMinepadUrl;
import net.montoyo.wd.net.server_bound.C2SMessageMinepadUrl;
import net.montoyo.wd.utilities.*;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@ -517,7 +517,7 @@ public class ClientProxy extends SharedProxy implements IDisplayHandler, IJSQuer
pd.view.loadURL(WebDisplays.BLACKLIST_URL);
else {
pd.lastURLSent = t; //Avoid spamming the server with porn URLs
WDNetworkRegistry.INSTANCE.sendToServer(new C2SMinepadUrl(pd.id, url));
WDNetworkRegistry.INSTANCE.sendToServer(new C2SMessageMinepadUrl(pd.id, url));
}
break;

View File

@ -18,8 +18,8 @@ import net.montoyo.wd.client.gui.loading.FillControl;
import net.montoyo.wd.entity.TileEntityScreen;
import net.montoyo.wd.item.ItemMinePad2;
import net.montoyo.wd.net.WDNetworkRegistry;
import net.montoyo.wd.net.server_bound.C2SMessageMinepadUrl;
import net.montoyo.wd.net.server_bound.C2SMessageScreenCtrl;
import net.montoyo.wd.net.server_bound.C2SMinepadUrl;
import net.montoyo.wd.utilities.BlockSide;
import net.montoyo.wd.utilities.Util;
import net.montoyo.wd.utilities.Vector3i;
@ -100,7 +100,7 @@ public class GuiSetURL2 extends WDScreen {
validate(tfURL.getText());
else if (ev.getSource() == btnShutDown) {
if (isPad) {
WDNetworkRegistry.INSTANCE.sendToServer(new C2SMinepadUrl(
WDNetworkRegistry.INSTANCE.sendToServer(new C2SMessageMinepadUrl(
getUUID(),
""
));
@ -130,7 +130,7 @@ public class GuiSetURL2 extends WDScreen {
if (isPad) {
UUID uuid = getUUID();
WDNetworkRegistry.INSTANCE.sendToServer(new C2SMinepadUrl(uuid, url));
WDNetworkRegistry.INSTANCE.sendToServer(new C2SMessageMinepadUrl(uuid, url));
stack.getTag().putString("PadURL", url);
ClientProxy.PadData pd = ((ClientProxy) WebDisplays.PROXY).getPadByID(uuid);

View File

@ -50,7 +50,8 @@ public class ScreenControlRegistry {
}
// if needed, the old code
// https://github.com/Mysticpasta1/webdisplays/blob/ff55cbf1b27773c15f44f17ad3364da3a16b6ed9/src/main/java/net/montoyo/wd/net/server/SMessageScreenCtrl.java
// https://github.com/Mysticpasta1/webdisplays/blob/ff55cbf1b27773c15f44f17ad3364da3a16b6ed9/src/main/java/net/montoyo/wd/net/server/SMessageScreenCtrl.java#L281-L364
// https://github.com/Mysticpasta1/webdisplays/blob/5ce9e4574df356910645b0382628f74d1401e26d/src/main/java/net/montoyo/wd/net/client_bound/S2CMessageScreenUpdate.java#L261-L284
static {
register(SetURLControl.id, new ScreenControlType<>(SetURLControl.class, SetURLControl::new));
register(KeyTypedControl.id, new ScreenControlType<>(KeyTypedControl.class, KeyTypedControl::new));
@ -60,6 +61,8 @@ public class ScreenControlRegistry {
register(ScreenModifyControl.id, new ScreenControlType<>(ScreenModifyControl.class, ScreenModifyControl::new));
register(ModifyFriendListControl.id, new ScreenControlType<>(ModifyFriendListControl.class, ModifyFriendListControl::new));
register(ManageRightsAndUpdgradesControl.id, new ScreenControlType<>(ManageRightsAndUpdgradesControl.class, ManageRightsAndUpdgradesControl::new));
register(ClickControl.id, new ScreenControlType<>(ClickControl.class, ClickControl::new));
register(OwnerControl.id, new ScreenControlType<>(OwnerControl.class, OwnerControl::new));
}
public static ScreenControl parse(FriendlyByteBuf buf) {

View File

@ -0,0 +1,62 @@
package net.montoyo.wd.controls.builtin;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.network.NetworkEvent;
import net.montoyo.wd.controls.ScreenControl;
import net.montoyo.wd.core.MissingPermissionException;
import net.montoyo.wd.entity.TileEntityScreen;
import net.montoyo.wd.utilities.BlockSide;
import net.montoyo.wd.utilities.Vector2i;
import java.util.function.Function;
public class ClickControl extends ScreenControl {
public static final ResourceLocation id = new ResourceLocation("webdisplays:click");
public enum ControlType {
CLICK, MOVE, DOWN, UP
}
ControlType type;
Vector2i coord;
public ClickControl(ControlType type, Vector2i coord) {
this(type, coord, -1);
this.type = type;
}
public ClickControl(ControlType type, Vector2i coord, int button) {
super(id);
this.coord = coord;
}
public ClickControl(FriendlyByteBuf buf) {
super(id);
type = ControlType.values()[buf.readByte()];
coord = new Vector2i(buf);
}
@Override
public void write(FriendlyByteBuf buf) {
buf.writeByte(type.ordinal());
coord.writeTo(buf);
}
@Override
public void handleServer(BlockPos pos, BlockSide side, TileEntityScreen tes, NetworkEvent.Context ctx, Function<Integer, Boolean> permissionChecker) throws MissingPermissionException {
throw new RuntimeException("Cannot call click control on server");
}
@Override
@OnlyIn(Dist.CLIENT)
public void handleClient(BlockPos pos, BlockSide side, TileEntityScreen tes, NetworkEvent.Context ctx) {
if (coord != null)
tes.handleMouseEvent(side, ClickControl.ControlType.MOVE, coord, -1);
tes.handleMouseEvent(side, type, coord, 1);
}
}

View File

@ -68,6 +68,12 @@ public class LaserControl extends ScreenControl {
@Override
@OnlyIn(Dist.CLIENT)
public void handleClient(BlockPos pos, BlockSide side, TileEntityScreen tes, NetworkEvent.Context ctx) {
throw new RuntimeException("TODO");
if (coord != null)
tes.handleMouseEvent(side, ClickControl.ControlType.MOVE, coord, -1);
switch (type) {
case UP -> tes.handleMouseEvent(side, ClickControl.ControlType.UP, coord, button);
case DOWN -> tes.handleMouseEvent(side, ClickControl.ControlType.DOWN, coord, button);
}
}
}

View File

@ -28,13 +28,15 @@ public class ManageRightsAndUpdgradesControl extends ScreenControl {
}
ControlType type;
boolean adding;
ItemStack toRemove;
private int friendRights;
private int otherRights;
public ManageRightsAndUpdgradesControl(ItemStack toRemove) {
public ManageRightsAndUpdgradesControl(boolean adding, ItemStack toRemove) {
super(id);
this.adding = adding;
type = ControlType.UPGRADES;
this.toRemove = toRemove;
}
@ -50,7 +52,10 @@ public class ManageRightsAndUpdgradesControl extends ScreenControl {
super(id);
type = ControlType.values()[buf.readByte()];
switch (type) {
case UPGRADES -> toRemove = buf.readItem();
case UPGRADES -> {
adding = buf.readBoolean();
toRemove = buf.readItem();
}
case RIGHTS -> {
friendRights = buf.readInt();
otherRights = buf.readInt();
@ -62,7 +67,10 @@ public class ManageRightsAndUpdgradesControl extends ScreenControl {
public void write(FriendlyByteBuf buf) {
buf.writeByte(type.ordinal());
switch (type) {
case UPGRADES -> buf.writeItem(toRemove);
case UPGRADES -> {
buf.writeBoolean(adding);
buf.writeItem(toRemove);
}
case RIGHTS -> {
buf.writeInt(friendRights);
buf.writeInt(otherRights);
@ -76,7 +84,9 @@ public class ManageRightsAndUpdgradesControl extends ScreenControl {
switch (type) {
case UPGRADES -> {
checkPerms(ScreenRights.MANAGE_UPGRADES, permissionChecker, ctx.getSender());
tes.removeUpgrade(side, toRemove, player);
if (adding)
throw new RuntimeException("Cannot add an upgrade from the client");
else tes.removeUpgrade(side, toRemove, player);
}
case RIGHTS -> {
TileEntityScreen.Screen scr = tes.getScreen(side);
@ -93,6 +103,22 @@ public class ManageRightsAndUpdgradesControl extends ScreenControl {
@Override
@OnlyIn(Dist.CLIENT)
public void handleClient(BlockPos pos, BlockSide side, TileEntityScreen tes, NetworkEvent.Context ctx) {
throw new RuntimeException("TODO");
ServerPlayer player = ctx.getSender();
switch (type) {
case UPGRADES -> {
if (adding)
tes.addUpgrade(side, toRemove, player, true);
else tes.removeUpgrade(side, toRemove, player);
}
case RIGHTS -> {
TileEntityScreen.Screen scr = tes.getScreen(side);
int fr = friendRights;
int or = otherRights;
if(scr.friendRights != fr || scr.otherRights != or)
tes.setRights(player, side, fr, or);
}
}
}
}

View File

@ -0,0 +1,47 @@
package net.montoyo.wd.controls.builtin;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.network.NetworkEvent;
import net.montoyo.wd.controls.ScreenControl;
import net.montoyo.wd.core.MissingPermissionException;
import net.montoyo.wd.entity.TileEntityScreen;
import net.montoyo.wd.utilities.BlockSide;
import net.montoyo.wd.utilities.NameUUIDPair;
import java.util.function.Function;
public class OwnerControl extends ScreenControl {
public static final ResourceLocation id = new ResourceLocation("webdisplays:set_owner");
NameUUIDPair owner;
public OwnerControl(NameUUIDPair pair) {
super(id);
this.owner = pair;
}
public OwnerControl(FriendlyByteBuf buf) {
super(id);
owner = new NameUUIDPair(buf);
}
@Override
public void write(FriendlyByteBuf buf) {
owner.writeTo(buf);
}
@Override
public void handleServer(BlockPos pos, BlockSide side, TileEntityScreen tes, NetworkEvent.Context ctx, Function<Integer, Boolean> permissionChecker) throws MissingPermissionException {
throw new RuntimeException("Cannot handle ownership theft packet from server");
}
@Override
@OnlyIn(Dist.CLIENT)
public void handleClient(BlockPos pos, BlockSide side, TileEntityScreen tes, NetworkEvent.Context ctx) {
tes.getScreen(side).owner = owner;
}
}

View File

@ -66,6 +66,9 @@ public class ScreenModifyControl extends ScreenControl {
@Override
@OnlyIn(Dist.CLIENT)
public void handleClient(BlockPos pos, BlockSide side, TileEntityScreen tes, NetworkEvent.Context ctx) {
throw new RuntimeException("TODO");
switch (type) {
case RESOLUTION -> tes.setResolution(side, res);
case ROTATION -> tes.setRotation(side, rotation);
}
}
}

View File

@ -27,6 +27,7 @@ import net.montoyo.wd.WebDisplays;
import net.montoyo.wd.block.BlockScreen;
import net.montoyo.wd.client.ClientProxy;
import net.montoyo.wd.config.CommonConfig;
import net.montoyo.wd.controls.builtin.ClickControl;
import net.montoyo.wd.core.DefaultUpgrade;
import net.montoyo.wd.core.IUpgrade;
import net.montoyo.wd.core.JSServerRequest;
@ -460,7 +461,7 @@ public class TileEntityScreen extends BlockEntity {
screens.get(idx).browser.close();
screens.get(idx).browser = null;
}
} else WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), new S2CMessageScreenUpdate(this, side)); //Delete the screen
} else WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), new S2CMessageScreenUpdate(this.getBlockPos(), side)); //Delete the screen
screens.remove(idx);
@ -531,17 +532,17 @@ public class TileEntityScreen extends BlockEntity {
if (level.isClientSide)
Log.warning("TileEntityScreen.click() from client side is useless...");
else if (getLaserUser(scr) == null)
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, S2CMessageScreenUpdate.MOUSE_CLICK, vec, 1));
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, ClickControl.ControlType.CLICK, vec));
}
void clickUnsafe(BlockSide side, int action, int x, int y) {
void clickUnsafe(BlockSide side, ClickControl.ControlType action, int x, int y) {
if (level.isClientSide) {
Vector2i vec = (action == S2CMessageScreenUpdate.MOUSE_UP) ? null : new Vector2i(x, y);
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, action, vec, 1));
Vector2i vec = (action == ClickControl.ControlType.UP) ? null : new Vector2i(x, y);
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, action, vec));
}
}
public void handleMouseEvent(BlockSide side, int event, @Nullable Vector2i vec, int button) {
public void handleMouseEvent(BlockSide side, ClickControl.ControlType event, @Nullable Vector2i vec, int button) {
Screen scr = getScreen(side);
if (scr == null) {
Log.error("Attempt inject mouse events on non-existing screen of side %s", side.toString());
@ -549,16 +550,16 @@ public class TileEntityScreen extends BlockEntity {
}
if (scr.browser != null) {
if (event == S2CMessageScreenUpdate.MOUSE_CLICK) {
if (event == ClickControl.ControlType.CLICK) {
scr.browser.injectMouseMove(vec.x, vec.y, 0, false); //Move to target
scr.browser.injectMouseButton(vec.x, vec.y, 0, button, true, 1); //Press
scr.browser.injectMouseButton(vec.x, vec.y, 0, button, false, 1); //Release
} else if (event == S2CMessageScreenUpdate.MOUSE_DOWN) {
} else if (event == ClickControl.ControlType.DOWN) {
scr.browser.injectMouseMove(vec.x, vec.y, 0, false); //Move to target
scr.browser.injectMouseButton(vec.x, vec.y, 0, button, true, 1); //Press
} else if (event == S2CMessageScreenUpdate.MOUSE_MOVE)
} else if (event == ClickControl.ControlType.MOVE)
scr.browser.injectMouseMove(vec.x, vec.y, 0, false); //Move
else if (event == S2CMessageScreenUpdate.MOUSE_UP)
else if (event == ClickControl.ControlType.UP)
scr.browser.injectMouseButton(scr.lastMousePos.x, scr.lastMousePos.y, 0, button, false, 1); //Release
if (vec != null) {
scr.lastMousePos.x = vec.x;
@ -592,8 +593,8 @@ public class TileEntityScreen extends BlockEntity {
}
}
if (sendMsg)
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.jsRedstone(this, side, vec, redstoneLevel));
// if (sendMsg)
// WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.jsRedstone(this, side, vec, redstoneLevel));
}
}
@ -926,8 +927,16 @@ public class TileEntityScreen extends BlockEntity {
//If equal is null, no duplicate check is preformed
public boolean addUpgrade(BlockSide side, ItemStack is, @Nullable Player player, boolean abortIfExisting) {
if (level.isClientSide)
if (level.isClientSide) {
IUpgrade itemAsUpgrade = (IUpgrade) is.getItem();
Screen scr = getScreen(side);
// if (abortIfExisting && scr.upgrades.stream().anyMatch(otherStack -> itemAsUpgrade.isSameUpgrade(is, otherStack)))
// return false; //Upgrade already exists
ItemStack isCopy = is.copy(); //FIXME: Duct tape fix, because the original stack will be shrinked
scr.upgrades.add(isCopy);
itemAsUpgrade.onInstall(this, side, player, isCopy);
return false;
}
Screen scr = getScreen(side);
if (scr == null) {
@ -953,9 +962,11 @@ public class TileEntityScreen extends BlockEntity {
isCopy.setCount(1);
scr.upgrades.add(isCopy);
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.upgrade(this, side));
itemAsUpgrade.onInstall(this, side, player, isCopy);
playSoundAt(WebDisplays.INSTANCE.soundUpgradeAdd, getBlockPos(), 1.0f, 1.0f);
if (player != null && !player.level.isClientSide) {
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.upgrade(this, side, true, is));
itemAsUpgrade.onInstall(this, side, player, isCopy);
playSoundAt(WebDisplays.INSTANCE.soundUpgradeAdd, getBlockPos(), 1.0f, 1.0f);
}
setChanged();
return true;
}
@ -1015,8 +1026,10 @@ public class TileEntityScreen extends BlockEntity {
if (idxToRemove >= 0) {
dropUpgrade(scr.upgrades.get(idxToRemove), side, player);
scr.upgrades.remove(idxToRemove);
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.upgrade(this, side));
playSoundAt(WebDisplays.INSTANCE.soundUpgradeDel, getBlockPos(), 1.0f, 1.0f);
if (player != null && !player.level.isClientSide) {
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.upgrade(this, side, false, is));
playSoundAt(WebDisplays.INSTANCE.soundUpgradeDel, getBlockPos(), 1.0f, 1.0f);
}
setChanged();
} else
Log.warning("Tried to remove non-existing upgrade %s to screen %s at %s", safeName(is), side.toString(), getBlockPos().toString());
@ -1068,11 +1081,11 @@ public class TileEntityScreen extends BlockEntity {
if (scr != null) {
if (button == -1)
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(ply, level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, S2CMessageScreenUpdate.MOUSE_MOVE, pos, button));
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(ply, level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, ClickControl.ControlType.MOVE, pos));
else if (down)
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(ply, level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, S2CMessageScreenUpdate.MOUSE_DOWN, pos, button));
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(ply, level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, ClickControl.ControlType.DOWN, pos));
else
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(ply, level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, S2CMessageScreenUpdate.MOUSE_UP, pos, button));
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(ply, level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, ClickControl.ControlType.UP, pos));
}
}
@ -1082,7 +1095,7 @@ public class TileEntityScreen extends BlockEntity {
if (scr != null) {
if (getLaserUser(scr) == ply) {
scr.laserUser = null;
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(ply, level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, S2CMessageScreenUpdate.MOUSE_UP, null, button));
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(ply, level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, ClickControl.ControlType.UP, null));
}
}
}
@ -1154,7 +1167,7 @@ public class TileEntityScreen extends BlockEntity {
if (scr.browser != null)
scr.browser.runJS(code, "");
}
else WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.js(this, side, code));
// else WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.js(this, side, code));
}
public void setAutoVolume(BlockSide side, boolean av) {

View File

@ -8,6 +8,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.montoyo.wd.WebDisplays;
@ -15,11 +16,11 @@ import net.montoyo.wd.block.BlockScreen;
import net.montoyo.wd.client.ClientProxy;
import net.montoyo.wd.client.JSQueryDispatcher;
import net.montoyo.wd.config.ClientConfig;
import net.montoyo.wd.controls.builtin.ClickControl;
import net.montoyo.wd.core.DefaultUpgrade;
import net.montoyo.wd.entity.TileEntityScreen;
import net.montoyo.wd.init.BlockInit;
import net.montoyo.wd.net.WDNetworkRegistry;
import net.montoyo.wd.net.client_bound.S2CMessageScreenUpdate;
import net.montoyo.wd.net.server_bound.C2SMessageScreenCtrl;
import net.montoyo.wd.utilities.BlockSide;
import net.montoyo.wd.utilities.Multiblock;
@ -83,7 +84,7 @@ public class ItemLaserPointer extends Item implements WDItem {
}
private static void laserClick(TileEntityScreen tes, BlockSide side, TileEntityScreen.Screen scr, Vector2i hit) {
tes.handleMouseEvent(side, S2CMessageScreenUpdate.MOUSE_MOVE, hit, -1);
tes.handleMouseEvent(side, ClickControl.ControlType.MOVE, hit, -1);
if (pointedScreen == tes && pointedScreenSide == side) {
long t = System.currentTimeMillis();
@ -125,16 +126,20 @@ public class ItemLaserPointer extends Item implements WDItem {
float hitZ = ((float) result.getLocation().z) - (float) pos.z;
Vector2i tmp = new Vector2i();
TileEntityScreen te = (TileEntityScreen) mc.level.getBlockEntity(pos.toBlock());
BlockEntity be = mc.level.getBlockEntity(pos.toBlock());
if (!(be instanceof TileEntityScreen)) return;
if (te != null && te.hasUpgrade(side, DefaultUpgrade.LASERMOUSE)) { //hasUpgrade returns false is there's no screen on side 'side'
//noinspection PatternVariableCanBeUsed
TileEntityScreen te = (TileEntityScreen) be;
if (te.hasUpgrade(side, DefaultUpgrade.LASERMOUSE)) { //hasUpgrade returns false is there's no screen on side 'side'
//Since rights aren't synchronized, let the server check them for us...
TileEntityScreen.Screen scr = te.getScreen(side);
if (scr.browser != null) {
if (BlockScreen.hit2pixels(side, result.getBlockPos(), new Vector3i(result.getBlockPos()), scr, hitX, hitY, hitZ, tmp)) {
te.handleMouseEvent(side, S2CMessageScreenUpdate.MOUSE_MOVE, tmp, -1);
te.handleMouseEvent(side, press ? S2CMessageScreenUpdate.MOUSE_DOWN : S2CMessageScreenUpdate.MOUSE_UP, tmp, button);
te.handleMouseEvent(side, ClickControl.ControlType.MOVE, tmp, -1);
te.handleMouseEvent(side, press ? ClickControl.ControlType.DOWN : ClickControl.ControlType.UP, tmp, button);
if (press)
WDNetworkRegistry.INSTANCE.sendToServer(C2SMessageScreenCtrl.laserDown(te, side, tmp, button));

View File

@ -21,7 +21,7 @@ import net.montoyo.wd.WebDisplays;
import net.montoyo.wd.config.CommonConfig;
import net.montoyo.wd.core.CraftComponent;
import net.montoyo.wd.net.WDNetworkRegistry;
import net.montoyo.wd.net.server_bound.C2SMinepadUrl;
import net.montoyo.wd.net.server_bound.C2SMessageMinepadUrl;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -64,7 +64,7 @@ public class ItemMinePad2 extends Item implements WDItem {
} else {
UUID uuid = UUID.randomUUID();
String url = getURL(is);
WDNetworkRegistry.INSTANCE.sendToServer(new C2SMinepadUrl(uuid, url));
WDNetworkRegistry.INSTANCE.sendToServer(new C2SMessageMinepadUrl(uuid, url));
is.getOrCreateTag().putUUID("PadID", uuid);
ok = true;

View File

@ -55,7 +55,7 @@ public class WDNetworkRegistry {
entries.add(new NetworkEntry<>(S2CMessageJSResponse.class, S2CMessageJSResponse::new));
// minepad
entries.add(new NetworkEntry<>(C2SMinepadUrl.class, C2SMinepadUrl::new));
entries.add(new NetworkEntry<>(C2SMessageMinepadUrl.class, C2SMessageMinepadUrl::new));
for (int i = 0; i < entries.size(); i++) entries.get(i).register(i, INSTANCE);
}

View File

@ -4,287 +4,133 @@
package net.montoyo.wd.net.client_bound;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkEvent;
import net.montoyo.wd.WebDisplays;
import net.montoyo.wd.block.BlockScreen;
import net.montoyo.wd.controls.ScreenControl;
import net.montoyo.wd.controls.ScreenControlRegistry;
import net.montoyo.wd.controls.builtin.*;
import net.montoyo.wd.entity.TileEntityScreen;
import net.montoyo.wd.net.BufferUtils;
import net.montoyo.wd.net.Packet;
import net.montoyo.wd.utilities.*;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
// TODO: use registry based approach
public class S2CMessageScreenUpdate extends Packet {
public static final int UPDATE_URL = 0;
public static final int UPDATE_RESOLUTION = 1;
public static final int UPDATE_DELETE = 2;
public static final int UPDATE_MOUSE = 3;
public static final int UPDATE_TYPE = 4;
public static final int UPDATE_UPGRADES = 5;
public static final int UPDATE_JS_REDSTONE = 6;
public static final int UPDATE_OWNER = 7;
public static final int UPDATE_ROTATION = 8;
public static final int UPDATE_RUN_JS = 9;
public static final int UPDATE_AUTO_VOL = 10;
public static final int MOUSE_CLICK = 0;
public static final int MOUSE_UP = 1;
public static final int MOUSE_MOVE = 2;
public static final int MOUSE_DOWN = 3;
private Vector3i pos;
private BlockSide side;
private int action;
private String string;
private Vector2i vec2i;
private int mouseEvent;
private int button;
private ItemStack[] upgrades;
private int redstoneLevel;
private NameUUIDPair owner;
private Rotation rotation;
private boolean autoVolume;
ScreenControl control;
BlockPos pos;
BlockSide side;
public S2CMessageScreenUpdate() {
}
public static S2CMessageScreenUpdate setURL(TileEntityScreen tes, BlockSide side, String url) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.action = UPDATE_URL;
ret.string = url;
return ret;
}
public static S2CMessageScreenUpdate setResolution(TileEntityScreen tes, BlockSide side, Vector2i res) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.action = UPDATE_RESOLUTION;
ret.vec2i = res;
return ret;
}
public static S2CMessageScreenUpdate click(TileEntityScreen tes, BlockSide side, int mouseEvent, @Nullable Vector2i pos, int button) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.action = UPDATE_MOUSE;
ret.mouseEvent = mouseEvent;
ret.vec2i = pos;
ret.button = button;
return ret;
}
public S2CMessageScreenUpdate(TileEntityScreen tes, BlockSide side) {
pos = new Vector3i(tes.getBlockPos());
public S2CMessageScreenUpdate(BlockPos blockPos, BlockSide side) {
this.pos = blockPos;
this.side = side;
action = UPDATE_DELETE;
}
public static S2CMessageScreenUpdate type(TileEntityScreen tes, BlockSide side, String text) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.string = text;
ret.action = UPDATE_TYPE;
return ret;
}
public static S2CMessageScreenUpdate js(TileEntityScreen tes, BlockSide side, String code) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.string = code;
ret.action = UPDATE_RUN_JS;
return ret;
}
public static S2CMessageScreenUpdate upgrade(TileEntityScreen tes, BlockSide side) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.action = UPDATE_UPGRADES;
ArrayList<ItemStack> upgrades = tes.getScreen(side).upgrades;
ret.upgrades = new ItemStack[upgrades.size()];
for(int i = 0; i < upgrades.size(); i++)
ret.upgrades[i] = upgrades.get(i).copy();
return ret;
}
public static S2CMessageScreenUpdate jsRedstone(TileEntityScreen tes, BlockSide side, Vector2i vec, int level) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.action = UPDATE_JS_REDSTONE;
ret.vec2i = vec;
ret.redstoneLevel = level;
return ret;
}
public static S2CMessageScreenUpdate owner(TileEntityScreen tes, BlockSide side, NameUUIDPair owner) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.action = UPDATE_OWNER;
ret.owner = owner;
return ret;
}
public static S2CMessageScreenUpdate rotation(TileEntityScreen tes, BlockSide side, Rotation rot) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.action = UPDATE_ROTATION;
ret.rotation = rot;
return ret;
}
public static S2CMessageScreenUpdate autoVolume(TileEntityScreen tes, BlockSide side, boolean av) {
S2CMessageScreenUpdate ret = new S2CMessageScreenUpdate();
ret.pos = new Vector3i(tes.getBlockPos());
ret.side = side;
ret.action = UPDATE_AUTO_VOL;
ret.autoVolume = av;
return ret;
}
public S2CMessageScreenUpdate(FriendlyByteBuf buf) {
super(buf);
Vector3i pos = new Vector3i(buf);
BlockSide side = BlockSide.values()[buf.readByte()];
byte action = buf.readByte();
S2CMessageScreenUpdate message = this;
message.pos = pos;
message.side = side;
message.action = action;
switch (action) {
case UPDATE_URL, UPDATE_TYPE, UPDATE_RUN_JS -> message.string = buf.readUtf();
case UPDATE_MOUSE -> {
message.mouseEvent = buf.readByte();
if (message.mouseEvent != MOUSE_UP)
message.vec2i = new Vector2i(buf);
if (message.mouseEvent != MOUSE_MOVE)
message.button = buf.readInt();
}
case UPDATE_RESOLUTION -> message.vec2i = new Vector2i(buf);
case UPDATE_UPGRADES -> {
message.upgrades = new ItemStack[buf.readByte()];
for (int i = 0; i < message.upgrades.length; i++)
message.upgrades[i] = buf.readItem();
}
case UPDATE_JS_REDSTONE -> {
message.vec2i = new Vector2i(buf);
message.redstoneLevel = buf.readByte();
}
case UPDATE_OWNER -> message.owner = new NameUUIDPair(buf);
case UPDATE_ROTATION -> message.rotation = Rotation.values()[buf.readByte() & 3];
case UPDATE_AUTO_VOL -> message.autoVolume = buf.readBoolean();
}
pos = buf.readBlockPos();
side = (BlockSide) BufferUtils.readEnum(buf, (i) -> BlockSide.values()[i], (byte) 1);
this.control = ScreenControlRegistry.parse(buf);
}
public static S2CMessageScreenUpdate setURL(TileEntityScreen screen, BlockSide side, String weburl) {
S2CMessageScreenUpdate screenUpdate = new S2CMessageScreenUpdate(screen.getBlockPos(), side);
screenUpdate.control = new SetURLControl(weburl, new Vector3i(screenUpdate.pos));
return screenUpdate;
}
public static S2CMessageScreenUpdate setResolution(TileEntityScreen screen, BlockSide side, Vector2i res) {
S2CMessageScreenUpdate screenUpdate = new S2CMessageScreenUpdate(screen.getBlockPos(), side);
screenUpdate.control = new ScreenModifyControl(res);
return screenUpdate;
}
public static S2CMessageScreenUpdate rotation(TileEntityScreen screen, BlockSide side, Rotation rot) {
S2CMessageScreenUpdate screenUpdate = new S2CMessageScreenUpdate(screen.getBlockPos(), side);
screenUpdate.control = new ScreenModifyControl(rot);
return screenUpdate;
}
public static S2CMessageScreenUpdate upgrade(TileEntityScreen screen, BlockSide side, boolean adding, ItemStack stack) {
S2CMessageScreenUpdate screenUpdate = new S2CMessageScreenUpdate(screen.getBlockPos(), side);
screenUpdate.control = new ManageRightsAndUpdgradesControl(adding, stack);
return screenUpdate;
}
public static S2CMessageScreenUpdate click(TileEntityScreen screen, BlockSide side, ClickControl.ControlType mouseMove, Vector2i pos) {
S2CMessageScreenUpdate screenUpdate = new S2CMessageScreenUpdate(screen.getBlockPos(), side);
screenUpdate.control = new ClickControl(mouseMove, pos);
return screenUpdate;
}
public static S2CMessageScreenUpdate type(TileEntityScreen screen, BlockSide side, String text) {
S2CMessageScreenUpdate screenUpdate = new S2CMessageScreenUpdate(screen.getBlockPos(), side);
screenUpdate.control = new KeyTypedControl(text, screenUpdate.pos);
return screenUpdate;
}
public static S2CMessageScreenUpdate autoVolume(TileEntityScreen screen, BlockSide side, boolean av) {
S2CMessageScreenUpdate screenUpdate = new S2CMessageScreenUpdate(screen.getBlockPos(), side);
screenUpdate.control = new AutoVolumeControl(av);
return screenUpdate;
}
public static S2CMessageScreenUpdate owner(TileEntityScreen screen, BlockSide side, NameUUIDPair owner) {
S2CMessageScreenUpdate screenUpdate = new S2CMessageScreenUpdate(screen.getBlockPos(), side);
screenUpdate.control = new OwnerControl(owner);
return screenUpdate;
}
@Override
public void write(FriendlyByteBuf buf) {
pos.writeTo(buf);
buf.writeByte(side.ordinal());
buf.writeByte(action);
if(action == UPDATE_URL || action == UPDATE_TYPE || action == UPDATE_RUN_JS)
buf.writeUtf(string);
else if(action == UPDATE_MOUSE) {
buf.writeByte(mouseEvent);
if(mouseEvent != MOUSE_UP)
vec2i.writeTo(buf);
if (mouseEvent != MOUSE_MOVE)
buf.writeInt(button);
} else if(action == UPDATE_RESOLUTION)
vec2i.writeTo(buf);
else if(action == UPDATE_UPGRADES) {
buf.writeByte(upgrades.length);
for(ItemStack is: upgrades)
buf.writeItem(is);
} else if(action == UPDATE_JS_REDSTONE) {
vec2i.writeTo(buf);
buf.writeByte(redstoneLevel);
} else if(action == UPDATE_OWNER)
owner.writeTo(buf);
else if(action == UPDATE_ROTATION)
buf.writeByte(rotation.ordinal());
else if(action == UPDATE_AUTO_VOL)
buf.writeBoolean(autoVolume);
buf.writeBlockPos(pos);
BufferUtils.writeEnum(buf, side, (byte) 1);
buf.writeUtf(control.getId().toString());
control.write(buf);
}
public void handle(NetworkEvent.Context ctx) {
if (checkClient(ctx)) {
ctx.enqueueWork(() -> {
BlockGetter level = WebDisplays.PROXY.getWorld(ctx);
if (level instanceof Level level1)
// ensure that the TE exists
level1.setBlock(
pos.toBlock(),
level.getBlockState(pos.toBlock()).setValue(BlockScreen.hasTE, true),11
);
BlockEntity te = level.getBlockEntity(pos.toBlock());
if(!(te instanceof TileEntityScreen)) {
Log.error("CMessageScreenUpdate: TileEntity at %s is not a screen!", pos.toString());
return;
}
TileEntityScreen tes = (TileEntityScreen) te;
switch (action) {
case UPDATE_URL -> {
try {
tes.setScreenURL(side, string);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
case UPDATE_MOUSE -> tes.handleMouseEvent(side, mouseEvent, vec2i, button);
case UPDATE_DELETE -> tes.removeScreen(side);
case UPDATE_RESOLUTION -> tes.setResolution(side, vec2i);
case UPDATE_TYPE -> tes.type(side, string, null);
case UPDATE_RUN_JS -> tes.evalJS(side, string);
case UPDATE_UPGRADES -> tes.updateUpgrades(side, upgrades);
case UPDATE_JS_REDSTONE -> tes.updateJSRedstone(side, vec2i, redstoneLevel);
case UPDATE_OWNER -> {
TileEntityScreen.Screen scr = tes.getScreen(side);
if (scr != null)
scr.owner = owner;
}
case UPDATE_ROTATION -> tes.setRotation(side, rotation);
case UPDATE_AUTO_VOL -> tes.setAutoVolume(side, autoVolume);
default -> Log.warning("Caught invalid CMessageScreenUpdate with action ID %d", action);
Level level = (Level) WebDisplays.PROXY.getWorld(ctx);
BlockEntity be = level.getBlockEntity(pos);
if (be instanceof TileEntityScreen tes) {
control.handleClient(pos, side, tes, ctx);
}
});
ctx.setPacketHandled(true);
// switch (action) {
// case UPDATE_URL -> {
// try {
// tes.setScreenURL(side, string);
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
// case UPDATE_MOUSE -> tes.handleMouseEvent(side, mouseEvent, vec2i, button);
// case UPDATE_DELETE -> tes.removeScreen(side);
// case UPDATE_RESOLUTION -> tes.setResolution(side, vec2i);
// case UPDATE_TYPE -> tes.type(side, string, null);
// case UPDATE_RUN_JS -> tes.evalJS(side, string);
// case UPDATE_UPGRADES -> tes.updateUpgrades(side, upgrades);
// case UPDATE_JS_REDSTONE -> tes.updateJSRedstone(side, vec2i, redstoneLevel);
// case UPDATE_OWNER -> {
// TileEntityScreen.Screen scr = tes.getScreen(side);
// if (scr != null)
// scr.owner = owner;
// }
// case UPDATE_ROTATION -> tes.setRotation(side, rotation);
// case UPDATE_AUTO_VOL -> tes.setAutoVolume(side, autoVolume);
// default -> Log.warning("Caught invalid CMessageScreenUpdate with action ID %d", action);
// }
}
}
}

View File

@ -9,16 +9,16 @@ import net.montoyo.wd.net.Packet;
import java.util.UUID;
public class C2SMinepadUrl extends Packet {
public class C2SMessageMinepadUrl extends Packet {
UUID id;
String url;
public C2SMinepadUrl(UUID id, String url) {
public C2SMessageMinepadUrl(UUID id, String url) {
this.id = id;
this.url = url;
}
public C2SMinepadUrl(FriendlyByteBuf buf) {
public C2SMessageMinepadUrl(FriendlyByteBuf buf) {
super(buf);
this.id = buf.readUUID();
this.url = buf.readUtf();

View File

@ -72,7 +72,7 @@ public class C2SMessageScreenCtrl extends Packet {
@Deprecated(forRemoval = true)
public C2SMessageScreenCtrl(TileEntityScreen tes, BlockSide side, ItemStack toRem) {
this(tes, side, new ManageRightsAndUpdgradesControl(toRem));
this(tes, side, new ManageRightsAndUpdgradesControl(false, toRem));
}
@Deprecated(forRemoval = true)
@ -173,6 +173,8 @@ public class C2SMessageScreenCtrl extends Packet {
});
}
} catch (MissingPermissionException e) {
e.printStackTrace();
} catch (Throwable ignored) {
}
});
ctx.setPacketHandled(true);

View File

@ -38,7 +38,7 @@
"webdisplays.message.tooBig": "Too big! Maximum size is %dx%d.",
"webdisplays.message.invalid": "Structure is invalid; look at %s.",
"webdisplays.message.turnOn": "You need to turn the screen on first!",
"webdisplays.message.screenSet": "Screen set! Now give the item to the new ow,ner...",
"webdisplays.message.screenSet": "Screen set! Now give the item to the new owner...",
"webdisplays.message.newOwner": "You are now the owner of this screen!",
"webdisplays.message.restrictions": "You are not allowed to do this :(",
"webdisplays.message.peripheral": "This is not a peripheral!",