63 lines
1.8 KiB
Java
63 lines
1.8 KiB
Java
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.ScreenBlockEntity;
|
|
import net.montoyo.wd.utilities.data.BlockSide;
|
|
import net.montoyo.wd.utilities.math.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, ScreenBlockEntity 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, ScreenBlockEntity tes, NetworkEvent.Context ctx) {
|
|
if (coord != null)
|
|
tes.handleMouseEvent(side, ClickControl.ControlType.MOVE, coord, -1);
|
|
|
|
tes.handleMouseEvent(side, type, coord, 1);
|
|
}
|
|
}
|