Skip to content

Commit

Permalink
Implements the /getid command
Browse files Browse the repository at this point in the history
  • Loading branch information
joserobjr committed Aug 19, 2020
1 parent c4b9c11 commit d63c61a
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 196 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>ExamplePlugin</name>
<artifactId>example-plugin</artifactId>
<name>GetIdPlugin</name>
<artifactId>getid-plugin</artifactId>
<groupId>org.powernukkit.plugins</groupId>
<version>0.1.0-SNAPSHOT</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<dependencies>
Expand Down
32 changes: 0 additions & 32 deletions src/main/java/org/powernukkit/plugins/example/CloneCommand.java

This file was deleted.

45 changes: 0 additions & 45 deletions src/main/java/org/powernukkit/plugins/example/CloneListener.java

This file was deleted.

52 changes: 0 additions & 52 deletions src/main/java/org/powernukkit/plugins/example/CloneMePlugin.java

This file was deleted.

70 changes: 70 additions & 0 deletions src/main/java/org/powernukkit/plugins/getid/GetIdCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.powernukkit.plugins.getid;

import cn.nukkit.block.BlockID;
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandExecutor;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.PluginCommand;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.item.ItemID;
import cn.nukkit.lang.TranslationContainer;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.OptionalInt;
import java.util.stream.Stream;

public class GetIdCommand implements CommandExecutor {
GetIdCommand(PluginCommand<?> command) {
command.setExecutor(this);
command.getCommandParameters().clear();
command.getCommandParameters().put("default", new CommandParameter[] {
new CommandParameter("blockOrItemName", false,
Stream.of(BlockID.class, ItemID.class)
.map(Class::getDeclaredFields)
.flatMap(Arrays::stream)
.filter(f-> f.getModifiers() == (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL))
.map(f-> f.getName().toLowerCase())
.toArray(String[]::new)
)
});
}

@Override
public boolean onCommand(CommandSender commandSender, Command command, String label, String[] args) {
if (args.length < 1 || args[0].trim().isEmpty()) {
commandSender.sendMessage(new TranslationContainer("commands.generic.usage", command.getUsage()));
}
String name = args[0].trim();
OptionalInt id = findId(ItemID.class, name);
if (!id.isPresent()) {
id = findId(BlockID.class, name);
if (id.isPresent()) {
int blockId = id.getAsInt();
if (blockId > 255) {
id = OptionalInt.of(255 - blockId);
}
}
}

if (!id.isPresent()) {
commandSender.sendMessage(name+" was not found");
} else {
commandSender.sendMessage(name + " = " + id.getAsInt());
}
return true;
}

private OptionalInt findId(Class<?> c, String name) {
return Arrays.stream(c.getDeclaredFields())
.filter(f -> f.getModifiers() == (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL))
.filter(f -> f.getName().equalsIgnoreCase(name))
.mapToInt(f -> {
try {
return f.getInt(null);
} catch (IllegalAccessException e) {
throw new InternalError(e);
}
}).findFirst();
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/powernukkit/plugins/getid/GetIdPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.powernukkit.plugins.getid;

import cn.nukkit.command.PluginCommand;
import cn.nukkit.plugin.PluginBase;

public class GetIdPlugin extends PluginBase {
@Override
public void onEnable() {
new GetIdCommand(((PluginCommand<?>) getCommand("getid")));
}
}
76 changes: 12 additions & 64 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,78 +1,26 @@
# Required
# The final name of your plugin, other plugins may use this name to declare dependency on your plugin
name: CloneMe
name: GetId

# Required
# The name of your class file that overrides PluginBase
main: org.powernukkit.plugins.example.CloneMePlugin
main: org.powernukkit.plugins.getid.GetIdPlugin

# Required
# The version of your plugin, it's recommended to follow the https://semver.org/ standard
version: "1.0.0"

# Required
# The minimum Cloudburst Nukkit API version, it's not the PowerNukkit version!
# Setting an outdated version usually don't cause issues so it's not a thing to worry too much
api: ["1.0.0"]
api: ["1.0.11"]

# Optional
# At which time your plugin will load.
# Valid options are: POSTWORLD, STARTUP
# Default value is POSTWORLD
load: POSTWORLD
author: joserobjr

# Optional
# Your name or your organization name or how people identify you
author: Nukkit Project
description: Simple plugin that shows the ID of a block using the /getid command

# Optional
# Explain in few words what this plugin does
description: Example plugin showing the API
website: https://github.com/PowerNukkit/getid-plugin

# Optional
# A link where the admins can find more info about your plugin
website: https://github.com/Nukkit/ExamplePlugin

# Optional
# Every sub-item must be a block, the key is the command name, you can create as many commands as you want
# Make sure to keep the exact same alignment, it is important
commands:
# The key is how the command will be used, like /cloneme . Avoid uppercase, the client game don't like it so much and may crash.
cloneme:
# Optional
# This information will be displayed in /help
description: Example command
getid:
description: Shows the ID of an item or block by it's name

# Optional
# This information will be displayed in /help and when your command executor returns false
usage: "/example"
usage: "/getid light_block"

# Optional
# The permission required to use your command. It's a good practice to always define one even if the command can be used by everybody
# More information below
permission: cloneme.cmd.use
permission: getid.cmd.use

# Optional
# Although you don't need to register your permissions here for them to work, it's good to allow the server owners to see all them quicker
# This section also allows to customize their behavior and provide more information about them
# You can also create group of permission
# Also, every sub-item must be a block just like the commands block above
permissions:
# The key must be the same value you use to define the permission
# It's a good practice to prefix it with your plugin name and create segmentation and then group them to make the permission management easier
cloneme.cmd.use:
# Optional
# This can be used by permission management plugins to show details about the permission key
description: "Allows the user to run the example command"

# Optional
# If a player don't have an allow-deny definition for this key, this default value will be used
# Valid options:
# - true: the permission is granted by default to everybody
# - false: the permission is revoked by default to everybody, including admins and OP
# - op: the permission is revoked by default to everybody, but is granted to OP players
# - notop: the permission is granted by default to everybody, but is revoked to OP players
# Default value is false
getid.cmd.use:
description: "Allows the user to run the getid command"
default: true

# TODO Add all possible configuration here

0 comments on commit d63c61a

Please sign in to comment.