Added Enchantment & PotionType enum + PlayerCommandEvent call

This commit is contained in:
Felix Cravic 2020-04-29 19:23:47 +02:00
parent c2af4be2fa
commit 0354a60635
8 changed files with 261 additions and 70 deletions

View File

@ -109,6 +109,8 @@ public class MinecraftServer {
RegistryMain.registerSounds();
RegistryMain.registerParticles();
RegistryMain.registerStats();
RegistryMain.registerEnchantments();
RegistryMain.registerPotions();
minecraftServer = new MinecraftServer();

View File

@ -4,6 +4,7 @@ import fr.themode.command.Command;
import fr.themode.command.CommandDispatcher;
import fr.themode.command.condition.CommandCondition;
import net.minestom.server.entity.Player;
import net.minestom.server.event.PlayerCommandEvent;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.utils.ArrayUtils;
@ -33,6 +34,14 @@ public class CommandManager {
if (command == null)
throw new NullPointerException("Command string cannot be null");
PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(source, command);
source.callEvent(PlayerCommandEvent.class, playerCommandEvent);
if (playerCommandEvent.isCancelled())
return false;
command = playerCommandEvent.getCommand();
try {
this.dispatcher.execute(source, command);
return true;

View File

@ -3,6 +3,7 @@ package net.minestom.server.inventory.type;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryProperty;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.potion.PotionType;
public class BeaconInventory extends Inventory {
@ -14,12 +15,12 @@ public class BeaconInventory extends Inventory {
sendProperty(InventoryProperty.BEACON_POWER_LEVEL, powerLevel);
}
public void setFirstPotionEffect(short firstPotionEffect) {
sendProperty(InventoryProperty.BEACON_FIRST_POTION, firstPotionEffect);
public void setFirstPotionEffect(PotionType firstPotionEffect) {
sendProperty(InventoryProperty.BEACON_FIRST_POTION, (short) firstPotionEffect.getId());
}
public void setSecondPotionEffect(short secondPotionEffect) {
sendProperty(InventoryProperty.BEACON_SECOND_POTION, secondPotionEffect);
public void setSecondPotionEffect(PotionType secondPotionEffect) {
sendProperty(InventoryProperty.BEACON_SECOND_POTION, (short) secondPotionEffect.getId());
}
}

View File

@ -0,0 +1,62 @@
package net.minestom.server.item;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
public enum Enchantment {
PROTECTION,
FIRE_PROTECTION,
FEATHER_FALLING,
BLAST_PROTECTION,
PROJECTILE_PROTECTION,
RESPIRATION,
AQUA_AFFINITY,
THORNS,
DEPTH_STRIDER,
FROST_WALKER,
BINDING_CURSE,
SHARPNESS,
SMITE,
BANE_OF_ARTHROPODS,
KNOCKBACK,
FIRE_ASPECT,
LOOTING,
SWEEPING,
EFFICIENCY,
SILK_TOUCH,
UNBREAKING,
FORTUNE,
POWER,
PUNCH,
FLAME,
INFINITY,
LUCK_OF_THE_SEA,
LURE,
LOYALTY,
IMPALING,
RIPTIDE,
CHANNELING,
MULTISHOT,
QUICK_CHARGE,
PIERCING,
MENDING,
VANISHING_CURSE;
private static Int2ObjectOpenHashMap<Enchantment> map = new Int2ObjectOpenHashMap();
private int id;
public static Enchantment fromId(int id) {
return map.get(id);
}
public void setIdentifier(int id) {
this.id = id;
map.put(id, this);
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,68 @@
package net.minestom.server.potion;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
public enum PotionType {
EMPTY,
WATER,
MUNDANE,
THICK,
AWKWARD,
NIGHT_VISION,
LONG_NIGHT_VISION,
INVISIBILITY,
LONG_INVISIBILITY,
LEAPING,
LONG_LEAPING,
STRONG_LEAPING,
FIRE_RESISTANCE,
LONG_FIRE_RESISTANCE,
SWIFTNESS,
LONG_SWIFTNESS,
STRONG_SWIFTNESS,
SLOWNESS,
LONG_SLOWNESS,
STRONG_SLOWNESS,
TURTLE_MASTER,
LONG_TURTLE_MASTER,
STRONG_TURTLE_MASTER,
WATER_BREATHING,
LONG_WATER_BREATHING,
HEALING,
STRONG_HEALING,
HARMING,
STRONG_HARMING,
POISON,
LONG_POISON,
STRONG_POISON,
REGENERATION,
LONG_REGENERATION,
STRONG_REGENERATION,
STRENGTH,
LONG_STRENGTH,
STRONG_STRENGTH,
WEAKNESS,
LONG_WEAKNESS,
LUCK,
SLOW_FALLING,
LONG_SLOW_FALLING;
private static Int2ObjectOpenHashMap<PotionType> map = new Int2ObjectOpenHashMap();
private int id;
public static PotionType fromId(int id) {
return map.get(id);
}
public void setIdentifier(int id) {
this.id = id;
map.put(id, this);
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,8 @@
package net.minestom.server.registry;
public class RegistryEnchantment {
protected String name;
protected int id;
}

View File

@ -6,8 +6,10 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minestom.server.entity.EntityType;
import net.minestom.server.instance.block.Block;
import net.minestom.server.item.Enchantment;
import net.minestom.server.item.Material;
import net.minestom.server.particle.Particle;
import net.minestom.server.potion.PotionType;
import net.minestom.server.sound.Sound;
import net.minestom.server.stat.StatisticType;
@ -35,11 +37,15 @@ public class RegistryMain {
List<RegistrySound> sounds = parseSounds(SOUNDS_PATH);
List<RegistryParticle> particles = parseParticles(PARTICLES_PATH);
List<RegistryStat> stats = parseStats(STATS_PATH);
List<RegistryEnchantment> enchantments = parseEnchantments(STATS_PATH);
List<RegistryPotion> potions = parsePotions(STATS_PATH);
//writeBlocksClass(blocks);
//writeItemsClass(items);
//writeEntitiesClass(entities);
//writeSoundsClass(sounds);
writeStatsClass(stats);
//writeStatsClass(stats);
//writeEnchantmentsClass(enchantments);
writePotionsClass(potions);
}
public static void registerBlocks() {
@ -117,6 +123,24 @@ public class RegistryMain {
}
}
public static void registerEnchantments() {
List<RegistryEnchantment> enchantments = parseEnchantments(STATS_PATH);
for (RegistryEnchantment registryEnchantment : enchantments) {
Enchantment enchantment = Enchantment.valueOf(registryEnchantment.name);
enchantment.setIdentifier(registryEnchantment.id);
}
}
public static void registerPotions() {
List<RegistryPotion> potions = parsePotions(STATS_PATH);
for (RegistryPotion registryPotion : potions) {
PotionType potionType = PotionType.valueOf(registryPotion.name);
potionType.setIdentifier(registryPotion.id);
}
}
private static void writeBlocksClass(List<RegistryBlock> blocks) {
for (RegistryBlock registryBlock : blocks) {
String line = registryBlock.name + ",";
@ -160,6 +184,20 @@ public class RegistryMain {
}
}
private static void writeEnchantmentsClass(List<RegistryEnchantment> enchantments) {
for (RegistryEnchantment registryEnchantment : enchantments) {
String line = registryEnchantment.name + ",";
System.out.println(line);
}
}
private static void writePotionsClass(List<RegistryPotion> potions) {
for (RegistryPotion registryPotion : potions) {
String line = registryPotion.name + ",";
System.out.println(line);
}
}
private static List<RegistryBlock> parseBlocks(String path) {
List<RegistryBlock> blocks = new ArrayList<>();
@ -231,21 +269,9 @@ public class RegistryMain {
}
private static List<RegistryItem> parseItems(String path) {
JsonObject entriesObject = parse(path, "minecraft:item");
List<RegistryItem> registryItems = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Gson gson = new Gson();
JsonObject obj = gson.fromJson(bufferedReader, JsonObject.class);
JsonObject itemsObject = obj.getAsJsonObject("minecraft:item");
JsonObject entriesObject = itemsObject.getAsJsonObject("entries");
Set<Map.Entry<String, JsonElement>> entriesEntries = entriesObject.entrySet();//will return members of your object
for (Map.Entry<String, JsonElement> entryEntry : entriesEntries) {
RegistryItem registryItem = new RegistryItem();
@ -261,21 +287,9 @@ public class RegistryMain {
}
private static List<RegistryEntityType> parseEntities(String path) {
JsonObject entriesObject = parse(path, "minecraft:entity_type");
List<RegistryEntityType> registryEntityTypes = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Gson gson = new Gson();
JsonObject obj = gson.fromJson(bufferedReader, JsonObject.class);
JsonObject itemsObject = obj.getAsJsonObject("minecraft:entity_type");
JsonObject entriesObject = itemsObject.getAsJsonObject("entries");
Set<Map.Entry<String, JsonElement>> entriesEntries = entriesObject.entrySet();//will return members of your object
for (Map.Entry<String, JsonElement> entryEntry : entriesEntries) {
RegistryEntityType registryEntityType = new RegistryEntityType();
@ -291,21 +305,9 @@ public class RegistryMain {
}
private static List<RegistrySound> parseSounds(String path) {
JsonObject entriesObject = parse(path, "minecraft:sound_event");
List<RegistrySound> registrySounds = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Gson gson = new Gson();
JsonObject obj = gson.fromJson(bufferedReader, JsonObject.class);
JsonObject itemsObject = obj.getAsJsonObject("minecraft:sound_event");
JsonObject entriesObject = itemsObject.getAsJsonObject("entries");
Set<Map.Entry<String, JsonElement>> entriesEntries = entriesObject.entrySet();//will return members of your object
for (Map.Entry<String, JsonElement> entryEntry : entriesEntries) {
RegistrySound registrySound = new RegistrySound();
@ -321,21 +323,9 @@ public class RegistryMain {
}
private static List<RegistryParticle> parseParticles(String path) {
JsonObject entriesObject = parse(path, "minecraft:particle_type");
List<RegistryParticle> registryParticles = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Gson gson = new Gson();
JsonObject obj = gson.fromJson(bufferedReader, JsonObject.class);
JsonObject itemsObject = obj.getAsJsonObject("minecraft:particle_type");
JsonObject entriesObject = itemsObject.getAsJsonObject("entries");
Set<Map.Entry<String, JsonElement>> entriesEntries = entriesObject.entrySet();//will return members of your object
for (Map.Entry<String, JsonElement> entryEntry : entriesEntries) {
RegistryParticle registryParticle = new RegistryParticle();
@ -351,21 +341,9 @@ public class RegistryMain {
}
private static List<RegistryStat> parseStats(String path) {
JsonObject entriesObject = parse(path, "minecraft:custom_stat");
List<RegistryStat> registryStats = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Gson gson = new Gson();
JsonObject obj = gson.fromJson(bufferedReader, JsonObject.class);
JsonObject itemsObject = obj.getAsJsonObject("minecraft:custom_stat");
JsonObject entriesObject = itemsObject.getAsJsonObject("entries");
Set<Map.Entry<String, JsonElement>> entriesEntries = entriesObject.entrySet();//will return members of your object
for (Map.Entry<String, JsonElement> entryEntry : entriesEntries) {
RegistryStat registryStat = new RegistryStat();
@ -380,4 +358,59 @@ public class RegistryMain {
return registryStats;
}
private static List<RegistryEnchantment> parseEnchantments(String path) {
JsonObject entriesObject = parse(path, "minecraft:enchantment");
List<RegistryEnchantment> registryEnchantments = new ArrayList<>();
Set<Map.Entry<String, JsonElement>> entriesEntries = entriesObject.entrySet();//will return members of your object
for (Map.Entry<String, JsonElement> entryEntry : entriesEntries) {
RegistryEnchantment registryEnchantment = new RegistryEnchantment();
registryEnchantments.add(registryEnchantment);
String item = entryEntry.getKey();
String itemName = item.toUpperCase().replace("MINECRAFT:", "").replace(".", "_");
registryEnchantment.name = itemName;
short id = entryEntry.getValue().getAsJsonObject().get("protocol_id").getAsShort();
registryEnchantment.id = id;
}
return registryEnchantments;
}
private static List<RegistryPotion> parsePotions(String path) {
JsonObject entriesObject = parse(path, "minecraft:potion");
List<RegistryPotion> registryPotions = new ArrayList<>();
Set<Map.Entry<String, JsonElement>> entriesEntries = entriesObject.entrySet();//will return members of your object
for (Map.Entry<String, JsonElement> entryEntry : entriesEntries) {
RegistryPotion registryPotion = new RegistryPotion();
registryPotions.add(registryPotion);
String item = entryEntry.getKey();
String itemName = item.toUpperCase().replace("MINECRAFT:", "").replace(".", "_");
registryPotion.name = itemName;
short id = entryEntry.getValue().getAsJsonObject().get("protocol_id").getAsShort();
registryPotion.id = id;
}
return registryPotions;
}
private static JsonObject parse(String path, String key) {
List<RegistryStat> registryStats = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Gson gson = new Gson();
JsonObject obj = gson.fromJson(bufferedReader, JsonObject.class);
JsonObject itemsObject = obj.getAsJsonObject(key);
JsonObject entriesObject = itemsObject.getAsJsonObject("entries");
return entriesObject;
}
}

View File

@ -0,0 +1,8 @@
package net.minestom.server.registry;
public class RegistryPotion {
protected String name;
protected int id;
}