generated from EpimorphicPioneers/ArchLoom-TemplateMod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from EpimorphicPioneers/dancingsnow-dev
让网络终端堆叠为1,添加几个网络修改的基础命令
- Loading branch information
Showing
7 changed files
with
131 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/com/epimorphismmc/eunetwork/common/data/EUNetCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.epimorphismmc.eunetwork.common.data; | ||
|
||
import com.epimorphismmc.eunetwork.EUNet; | ||
import com.epimorphismmc.eunetwork.common.EUNetworkBase; | ||
import com.epimorphismmc.eunetwork.common.EUNetworkData; | ||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.arguments.IntegerArgumentType; | ||
import com.mojang.brigadier.arguments.StringArgumentType; | ||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
import com.mojang.brigadier.builder.RequiredArgumentBuilder; | ||
import net.minecraft.commands.CommandSourceStack; | ||
import net.minecraft.commands.Commands; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.server.level.ServerPlayer; | ||
|
||
import java.math.BigInteger; | ||
import java.text.NumberFormat; | ||
|
||
public class EUNetCommands { | ||
private static final NumberFormat nf = NumberFormat.getInstance(); | ||
|
||
private static final RequiredArgumentBuilder<CommandSourceStack, Integer> NETWORK_ARGUMENT = | ||
Commands.argument("id", IntegerArgumentType.integer()) | ||
.suggests((ctx, builder) -> { | ||
EUNetworkData.getAllNetworks().forEach(n -> builder.suggest(n.getId())); | ||
return builder.buildFuture(); | ||
}); | ||
|
||
private static final LiteralArgumentBuilder<CommandSourceStack> root = Commands.literal(EUNet.MODID) | ||
.requires(src -> src.hasPermission(2)) | ||
.then(Commands.literal("get") | ||
.executes(src -> { | ||
ServerPlayer player = src.getSource().getPlayerOrException(); | ||
for (EUNetworkBase network : EUNetworkData.getAllNetworks()) { | ||
if (network.canPlayerAccess(player)) { | ||
src.getSource().sendSuccess( | ||
() -> Component.translatable("message.eunetwork.network_id", network.getId()), | ||
true | ||
); | ||
src.getSource().sendSuccess( | ||
() -> Component.translatable("message.eunetwork.network_storage", nf.format(network.getStorage())), | ||
true | ||
); | ||
} | ||
} | ||
return 1; | ||
}) | ||
.then(NETWORK_ARGUMENT | ||
.executes(src -> { | ||
int id = IntegerArgumentType.getInteger(src, "id"); | ||
EUNetworkBase network = EUNetworkData.getNetwork(id); | ||
if (network == null) { | ||
src.getSource().sendFailure(Component.translatable("message.eunetwork.invalid_network", id)); | ||
} else { | ||
src.getSource().sendSuccess( | ||
() -> Component.translatable("message.eunetwork.network_id", network.getId()), | ||
true | ||
); | ||
src.getSource().sendSuccess( | ||
() -> Component.translatable("message.eunetwork.network_storage", nf.format(network.getStorage())), | ||
true | ||
); | ||
} | ||
return 1; | ||
}))) | ||
.then(Commands.literal("add") | ||
.then(NETWORK_ARGUMENT | ||
.then(Commands.argument("value", StringArgumentType.string()) | ||
.executes(src -> { | ||
int id = IntegerArgumentType.getInteger(src, "id"); | ||
String valueString = StringArgumentType.getString(src, "value"); | ||
BigInteger value; | ||
try { | ||
value = new BigInteger(valueString); | ||
} catch (NumberFormatException e) { | ||
src.getSource().sendFailure(Component.translatable("message.eunetwork.invalid_number", valueString)); | ||
return 0; | ||
} | ||
EUNetworkBase network = EUNetworkData.getNetwork(id); | ||
if (network == null) { | ||
src.getSource().sendFailure(Component.translatable("message.eunetwork.invalid_network", id)); | ||
} else { | ||
BigInteger inserted = network.addEnergy(value); | ||
src.getSource().sendSuccess( | ||
() -> Component.translatable("message.eunetwork.add_successed", id, nf.format(inserted)), | ||
true | ||
); | ||
} | ||
return 1; | ||
}))) | ||
); | ||
|
||
public static void init(CommandDispatcher<CommandSourceStack> dispatcher) { | ||
dispatcher.register(root); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters