Skip to content

Commit

Permalink
more commands, need to list devices and create configuration card next
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushmead committed Jul 29, 2024
1 parent 1021cc8 commit dfb7a1f
Show file tree
Hide file tree
Showing 25 changed files with 720 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.20.2 2024-07-20T12:53:36.9764242 Languages: en_us
e3979f69d6f26453fdf1227dc1d640cc4143c3ff assets/theatrical/lang/en_us.json
// 1.20.2 2024-07-29T23:11:39.1659592 Languages: en_us
51f24e3b8b2d0667dec3cce6b38b13b241264dc8 assets/theatrical/lang/en_us.json
14 changes: 14 additions & 0 deletions common/src/generated/resources/assets/theatrical/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@
"block.theatrical.redstone_interface": "Redstone Interface",
"block.theatrical.tank_trap": "Tank Trap",
"block.theatrical.truss": "MT100 Truss",
"button.artnetconfig": "ArtNet Config",
"commands.network": "%s (%s) has %s member(s)",
"commands.network.created": "Network created",
"commands.network.deleted": "Network deleted",
"commands.network.invalid": "Unknown network mode: %s",
"commands.network.members": "There are %s network member(s): %s.",
"commands.network.members.add.failed": "Player already member of network.",
"commands.network.members.add.success": "Added %s to the network.",
"commands.network.members.remove.success": "Removed %s from the network.",
"commands.network.notfound": "Network not found.",
"commands.network.role.invalid": "Unknown member role: %s",
"commands.network.updated": "Network updated",
"commands.networks": "There are %s network(s): %s.",
"fixture.dmxStart": "DMX Address",
"fixture.pan": "Pan",
"fixture.tilt": "Tilt",
"itemGroup.theatrical": "Theatrical",
"screen.artnetconfig.enabled": "ArtNet Enabled: %s",
"screen.movinglight": "Moving Light",
"ui.control.cue": "Cue - %s",
"ui.control.cues": "Cues",
Expand Down
88 changes: 29 additions & 59 deletions common/src/main/java/dev/imabad/theatrical/Theatrical.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
package dev.imabad.theatrical;

import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.architectury.event.events.common.CommandRegistrationEvent;
import dev.architectury.event.events.common.LifecycleEvent;
import dev.architectury.event.events.common.PlayerEvent;
import dev.architectury.platform.Platform;
import dev.architectury.registry.CreativeTabRegistry;
import dev.architectury.registry.registries.DeferredRegister;
import dev.architectury.registry.registries.Registrar;
import dev.architectury.registry.registries.RegistrySupplier;
import dev.imabad.theatrical.blockentities.BlockEntities;
import dev.imabad.theatrical.blocks.Blocks;
import dev.imabad.theatrical.commands.CommandArguments;
import dev.imabad.theatrical.commands.DMXNetworkModeArgument;
import dev.imabad.theatrical.commands.MemberRoleArgument;
import dev.imabad.theatrical.commands.NetworkCommand;
import dev.imabad.theatrical.config.ConfigHandler;
import dev.imabad.theatrical.config.TheatricalConfig;
import dev.imabad.theatrical.dmx.DMXDevice;
import dev.imabad.theatrical.dmx.DMXNetwork;
import dev.imabad.theatrical.dmx.DMXNetworkData;
import dev.imabad.theatrical.dmx.DMXNetworkMember;
import dev.imabad.theatrical.dmx.*;
import dev.imabad.theatrical.fixtures.Fixtures;
import dev.imabad.theatrical.items.Items;
import dev.imabad.theatrical.mixin.ArgumentTypeInfosAccessor;
import dev.imabad.theatrical.net.artnet.ListConsumers;
import dev.imabad.theatrical.net.TheatricalNet;
import net.minecraft.commands.Commands;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.server.players.GameProfileCache;
import net.minecraft.util.Tuple;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.text.html.Option;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

public class Theatrical {
public static final String MOD_ID = "theatrical";
Expand All @@ -60,6 +59,11 @@ public static void init() {
TheatricalNet.init();
Blocks.BLOCKS.register();
BlockEntities.BLOCK_ENTITIES.register();

DeferredRegister<ArgumentTypeInfo<?, ?>> argTypes = TheatricalRegistry.get(Registries.COMMAND_ARGUMENT_TYPE);
registerArgument(argTypes, SingletonArgumentInfo.contextFree(DMXNetworkModeArgument::networkMode), "network_mode", DMXNetworkModeArgument.class);
registerArgument(argTypes, SingletonArgumentInfo.contextFree(MemberRoleArgument::memberRole), "member_role", MemberRoleArgument.class);

dev.imabad.theatrical.items.Items.ITEMS.register();
PlayerEvent.PLAYER_JOIN.register((event) -> {
DMXNetworkData instance = DMXNetworkData.getInstance(event.server.overworld());
Expand All @@ -75,53 +79,19 @@ public static void init() {
}
}
});
LifecycleEvent.SERVER_LEVEL_UNLOAD.register(world -> {
if(world.dimension().equals(Level.OVERWORLD)){
DMXNetworkData.unloadLevel();
}
});
CommandRegistrationEvent.EVENT.register((dispatcher, registry, selection) -> {
dispatcher.register(Commands.literal("theatrical")
.then(Commands.literal("networks")
.executes(context -> {
DMXNetworkData instance = DMXNetworkData.
getInstance(context.getSource().getServer().overworld());
String networks;
if(context.getSource().hasPermission(context.getSource().getServer().getOperatorUserPermissionLevel())){
networks = instance.getAllNetworks().stream()
.map(DMXNetwork::name).collect(Collectors.joining(","));
} else {
networks = instance.getNetworksForPlayer(context.getSource().getPlayer().getUUID()).stream()
.map(DMXNetwork::name).collect(Collectors.joining(","));
}
context.getSource().sendSystemMessage(Component.literal("Networks: " + networks));
return 1;
})
)
.then(Commands.literal("network")
.then(Commands.argument("id", StringArgumentType.string())
.then(Commands.literal("members")
.executes(context -> {
DMXNetworkData instance = DMXNetworkData.
getInstance(context.getSource().getServer().overworld());
try {
String id = context.getArgument("id", String.class);
UUID uuid = UUID.fromString(id);
DMXNetwork network = instance.getNetwork(uuid);
if(network != null) {
GameProfileCache profileCache = context.getSource().getServer().getProfileCache();
String collect = network.members()
.stream().map(DMXNetworkMember::playerId)
.map((playerUUID) -> new Tuple<>(playerUUID, profileCache.get(playerUUID)))
.map(uuidOptionalTuple -> uuidOptionalTuple.getB().isPresent() ? uuidOptionalTuple.getB().get().getName() : uuidOptionalTuple.getA().toString())
.toList().stream().collect(Collectors.joining(","));
context.getSource().sendSystemMessage(Component.literal("Members: " + collect));
}
} catch (Exception e){
context.getSource().sendSystemMessage(Component.literal("Invalid network"));
return -1;
}
return 1;
})
)
)
)
);
NetworkCommand.register(dispatcher);
});
}

private static void registerArgument(DeferredRegister<ArgumentTypeInfo<?, ?>> argTypes,
ArgumentTypeInfo<?, ?> serializer, String id, Class<?> clazz) {
argTypes.register(new ResourceLocation(Theatrical.MOD_ID, id), () -> serializer);
ArgumentTypeInfosAccessor.classMap().put(clazz, serializer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void tick(){

public void update(byte[] data) {
if(level != null && level.getServer() != null) {
var dmxData = DMXNetworkData.getInstance(level).getNetwork(networkId);
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if(dmxData != null) {
dmxData.getConsumers(universe).forEach(consumer -> consumer.consume(data));
}
Expand Down Expand Up @@ -195,6 +195,7 @@ public void read(CompoundTag compoundTag) {

public void setFaders(byte[] faders){
this.faders = Arrays.copyOf(faders, faders.length);
setChanged();
}

public void setFader(int fader, int value){
Expand All @@ -203,6 +204,7 @@ public void setFader(int fader, int value){
} else {
this.grandMaster = (byte) value;
}
setChanged();
}

public int getCurrentStep() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void read(CompoundTag compoundTag) {

public void update(byte[] data) {
if(level != null && level.getServer() != null) {
var dmxData = DMXNetworkData.getInstance(level).getNetwork(networkId);
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if(dmxData != null) {
dmxData.getConsumersInRange(universe, getBlockPos(), TheatricalConfig.INSTANCE.COMMON.wirelessDMXRadius).forEach(dmxConsumer -> dmxConsumer.consume(data));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ public void setUniverse(int universe) {
}

private void updateConsumer(){
var dmxData = DMXNetworkData.getInstance(level).getNetwork(networkId);
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if (dmxData != null) {
dmxData.updateConsumer(this);
}
}

private void addConsumer(){
var dmxData = DMXNetworkData.getInstance(level).getNetwork(networkId);
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if (dmxData != null) {
if(deviceId == null){
generateDeviceId();
Expand All @@ -179,7 +179,7 @@ private void addConsumer(){
}

private void removeConsumer(){
var dmxData = DMXNetworkData.getInstance(level).getNetwork(networkId);
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if (dmxData != null) {
dmxData.removeConsumer(this, getBlockPos());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ public void setChannelStartPoint(int channelStartPoint) {
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_CLIENTS);
}
private void updateConsumer(){
var dmxData = DMXNetworkData.getInstance(level).getNetwork(networkId);
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if (dmxData != null) {
dmxData.updateConsumer(this);
}
}
private void removeConsumer(){
var dmxData = DMXNetworkData.getInstance(level).getNetwork(networkId);
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if (dmxData != null) {
dmxData.removeConsumer(this, getBlockPos());
}
}
private void addConsumer(){
var dmxData = DMXNetworkData.getInstance(level).getNetwork(networkId);
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if (dmxData != null) {
if(deviceId == null){
generateDeviceId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable L
if(!level.isClientSide){
BlockEntity be = level.getBlockEntity(pos);
if(be instanceof BaseDMXConsumerLightBlockEntity consumerLightBlockEntity && placer instanceof ServerPlayer player){
consumerLightBlockEntity.setNetworkId(DMXNetworkData.getInstance(level).getDefaultNetworkForPlayer(player).id());
consumerLightBlockEntity.setNetworkId(DMXNetworkData.getInstance(level.getServer().overworld()).getDefaultNetworkForPlayer(player).id());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected void init() {
).withValues(CycleButton.ValueListSupplier.create(Stream.concat(Stream.of(UUIDUtil.NULL),
TheatricalClient.getArtNetManager().getKnownNetworks().keySet().stream()).collect(Collectors.toList())))
.displayOnlyValue().withInitialValue(networkId)
.create(xCenter, yCenter, 150, 20,
.create(xCenter + 45, yCenter + 130, 150, 20,
Component.translatable("screen.artnetconfig.network"), (obj, val) -> {
this.networkId = val;
new UpdateNetworkId(be.getBlockPos(), networkId).sendToServer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.imabad.theatrical.commands;

import dev.architectury.registry.registries.DeferredRegister;
import dev.architectury.registry.registries.RegistrySupplier;
import dev.imabad.theatrical.TheatricalRegistry;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
import net.minecraft.core.registries.Registries;

public class CommandArguments {

public static final DeferredRegister<ArgumentTypeInfo<?, ?>> ARGUMENTS = TheatricalRegistry.get(Registries.COMMAND_ARGUMENT_TYPE);

public static final RegistrySupplier<ArgumentTypeInfo<DMXNetworkModeArgument, ?>> NETWORK_MODE = ARGUMENTS.register("network_mode", () -> SingletonArgumentInfo.contextFree(DMXNetworkModeArgument::networkMode));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dev.imabad.theatrical.commands;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import dev.imabad.theatrical.dmx.DMXNetworkMode;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.Component;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

public class DMXNetworkModeArgument implements ArgumentType<DMXNetworkMode> {
private static final DynamicCommandExceptionType ERROR_INVALID = new DynamicCommandExceptionType(
object -> Component.translatable("commands.network.invalid", object)
);
private static final DMXNetworkMode[] VALUES = DMXNetworkMode.values();

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> commandContext, SuggestionsBuilder suggestionsBuilder) {
return commandContext.getSource() instanceof SharedSuggestionProvider
? SharedSuggestionProvider.suggest(Arrays.stream(VALUES).map(DMXNetworkMode::getName), suggestionsBuilder)
: Suggestions.empty();
}

@Override
public DMXNetworkMode parse(StringReader reader) throws CommandSyntaxException {
String string = reader.readUnquotedString();
DMXNetworkMode networkMode = DMXNetworkMode.byName(string);
if (networkMode == null) {
throw ERROR_INVALID.createWithContext(reader, string);
} else {
return networkMode;
}
}

public static DMXNetworkModeArgument networkMode() {
return new DMXNetworkModeArgument();
}
public static DMXNetworkMode getMode(CommandContext<CommandSourceStack> context, String name) {
return context.getArgument(name, DMXNetworkMode.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dev.imabad.theatrical.commands;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import dev.imabad.theatrical.dmx.DMXNetworkMemberRole;
import dev.imabad.theatrical.dmx.DMXNetworkMode;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.Component;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

public class MemberRoleArgument implements ArgumentType<DMXNetworkMemberRole> {
private static final DynamicCommandExceptionType ERROR_INVALID = new DynamicCommandExceptionType(
object -> Component.translatable("commands.network.role.invalid", object)
);
private static final DMXNetworkMemberRole[] VALUES = DMXNetworkMemberRole.values();

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> commandContext, SuggestionsBuilder suggestionsBuilder) {
return commandContext.getSource() instanceof SharedSuggestionProvider
? SharedSuggestionProvider.suggest(Arrays.stream(VALUES).map(DMXNetworkMemberRole::getName), suggestionsBuilder)
: Suggestions.empty();
}

@Override
public DMXNetworkMemberRole parse(StringReader reader) throws CommandSyntaxException {
String string = reader.readUnquotedString();
DMXNetworkMemberRole networkMode = DMXNetworkMemberRole.byName(string);
if (networkMode == null) {
throw ERROR_INVALID.createWithContext(reader, string);
} else {
return networkMode;
}
}

public static MemberRoleArgument memberRole() {
return new MemberRoleArgument();
}
public static DMXNetworkMemberRole getMode(CommandContext<CommandSourceStack> context, String name) {
return context.getArgument(name, DMXNetworkMemberRole.class);
}
}
Loading

0 comments on commit dfb7a1f

Please sign in to comment.