Skip to content

Commit

Permalink
Performance update
Browse files Browse the repository at this point in the history
  • Loading branch information
Furkan Mercimek committed Sep 18, 2018
1 parent 04f1ede commit f845bd6
Show file tree
Hide file tree
Showing 27 changed files with 399 additions and 232 deletions.
4 changes: 2 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: skript-nms
author: FurkannM
description: A Skript addon for NMS stuff
version: 0.0.1
version: 0.1.0
main: com.furkannm.skriptnms.Core
softdepend: [Skript]
depend: [Skript]
77 changes: 62 additions & 15 deletions src/com/furkannm/skriptnms/Core.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,80 @@
package com.furkannm.skriptnms;

import java.io.IOException;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.ExpressionType;
import com.furkannm.skriptnms.util.nms.NMSClasses;
import com.furkannm.skriptnms.util.nms.types.*;

import com.furkannm.skriptnms.effects.EffLoadDat;
import com.furkannm.skriptnms.expressions.ExprFileNbt;
import com.furkannm.skriptnms.expressions.ExprNBTOf;
import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon;

public class Core extends JavaPlugin{
static String ver;
private static String ver;
private static Core instance;
private static SkriptAddon addonInstance;

public Core() {
if(instance == null) {
instance = this;
}else{
throw new IllegalStateException();
}
}

@Override
public void onEnable() {
if (Bukkit.getPluginManager().getPlugin("Skript") != null && Skript.isAcceptRegistrations()) {
Skript.registerExpression(ExprNBTOf.class, Object.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %~object%", "%~object%'s nbt[[ ]tag[s]]");
Skript.registerExpression(ExprFileNbt.class, Object.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] from [file] %string%", "nbt[[ ]tag[s]] from last loaded [dat ]file", "last loaded [dat ]file's nbt[[ ]tag[s]]");
Skript.registerEffect(EffLoadDat.class, "load nbt[[ ]tag[s]] from [file] %string%","load %string%'s nbt[[ ]tag[s]]");
getLogger().info("Skript-NMS is started!");
ver = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
}else {
getLogger().info("Unable to find Skript or Skript isn't accepting registrations, disabling skript-nms...");
Bukkit.getPluginManager().disablePlugin(this);
getLogger().info("Skript-NMS is started!");
ver = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
try {
getAddonInstance().loadClasses("com.furkannm.skriptnms", "effects", "expressions");
} catch (IOException e) {
e.printStackTrace();
}
loadClass();
NMSClasses.loadClasses();
}

public static SkriptAddon getAddonInstance() {
if(addonInstance == null) {
addonInstance = Skript.registerAddon(getInstance());
}
return addonInstance;
}

public static Core getInstance() {
if(instance == null) {
throw new IllegalStateException();
}
return instance;
}

public static String getVer() {
return ver;
}

public static void loadClass() {
new BlockPosition();
new CraftEntity();
new CraftItemStack();
new CraftWorld();
new Entity();
new IBlockData();
new ItemStack();
new MojangsonParser();
new NBTBase();
new NBTCompressedStreamTools();
new NBTTagByte();
new NBTTagCompound();
new NBTTagDouble();
new NBTTagFloat();
new NBTTagInt();
new NBTTagLong();
new NBTTagShort();
new NBTTagString();
new TileEntity();
new World();
}
}
4 changes: 4 additions & 0 deletions src/com/furkannm/skriptnms/effects/EffLoadDat.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

public class EffLoadDat extends Effect {

static {
Skript.registerEffect(EffLoadDat.class, "load nbt[[ ]tag[s]] from [file] %string%","load %string%'s nbt[[ ]tag[s]]");
}

//load dat file in %string%
private File lastLoadedFile = null;
private Expression<String> fileName;
Expand Down
14 changes: 10 additions & 4 deletions src/com/furkannm/skriptnms/expressions/ExprFileNbt.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@

import org.bukkit.event.Event;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;

import com.furkannm.skriptnms.util.nms.NBTTagCompound;
import com.furkannm.skriptnms.util.nms.DatFile;
import com.furkannm.skriptnms.util.nms.NMS;
import com.furkannm.skriptnms.util.nms.types.DatFile;
import com.furkannm.skriptnms.util.nms.types.NBTTagCompound;

@Name("File NBT")
@Examples({
Expand All @@ -27,6 +29,10 @@

public class ExprFileNbt extends SimpleExpression<Object>{

static {
Skript.registerExpression(ExprFileNbt.class, Object.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] from [file] %string%", "nbt[[ ]tag[s]] from last loaded [dat ]file", "last loaded [dat ]file's nbt[[ ]tag[s]]");
}

private Expression<Object> target;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -85,11 +91,11 @@ public void change(Event e, Object[] args, ChangeMode mode) {
if(target != null) {
Object f = target.getSingle(e);
f = f.toString().endsWith(".dat") ? f.toString() : f.toString() + ".dat";
file = new File(f.toString());

file = new File(f.toString());
}else{
file = DatFile.getFile();
}

Object parsedNBT = null;
if (args != null) {
parsedNBT = NBTTagCompound.get().cast(NMS.parseRawNBT(((String) args[0])));
Expand Down
74 changes: 13 additions & 61 deletions src/com/furkannm/skriptnms/expressions/ExprNBTOf.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.furkannm.skriptnms.expressions;

import java.util.Arrays;

import javax.annotation.Nullable;

import org.bukkit.block.Block;
Expand All @@ -14,15 +12,16 @@
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.skript.util.slot.Slot;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;

import com.furkannm.skriptnms.util.nms.NBTTagCompound;
import com.furkannm.skriptnms.util.nms.NMS;
import com.furkannm.skriptnms.util.nms.types.NBTTagCompound;

@Name("NBT of")
@Examples({
Expand All @@ -32,6 +31,10 @@

public class ExprNBTOf extends SimpleExpression<Object> {

static {
Skript.registerExpression(ExprNBTOf.class, Object.class, ExpressionType.PROPERTY, "nbt[[ ]tag[s]] of %~object%", "%~object%'s nbt[[ ]tag[s]]");
}

private Expression<Object> target;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -65,16 +68,7 @@ public String toString(@Nullable Event e, boolean debug) {
@Nullable
protected Object[] get(Event e) {
Object tar = target.getSingle(e);
if (tar instanceof Entity) {
return new Object[] { NBTTagCompound.get().cast(NMS.getEntityNBT((Entity) tar)) };
} else if (tar instanceof Block) {
return new Object[] { NBTTagCompound.get().cast(NMS.getTileNBT((Block) tar)) };
} else if (tar instanceof ItemStack) {
return new Object[] { NBTTagCompound.get().cast(NMS.getItemNBT((ItemStack) tar)) };
} else if (tar instanceof Slot) {
return new Object[] { NBTTagCompound.get().cast(NMS.getItemNBT(((Slot) tar).getItem())) };
}
return null;
return NMS.getNBT(tar);
}

@Override
Expand All @@ -84,54 +78,12 @@ public void change(Event e, Object[] args, ChangeMode mode) {
if (args != null) {
parsedNBT = NBTTagCompound.get().cast(NMS.parseRawNBT(((String) args[0])));
}
if (tar instanceof Entity) {
Object entNBT = NBTTagCompound.get().cast(NMS.getEntityNBT((Entity) tar));
if (mode == ChangeMode.ADD) {
NMS.removeFromCompound(NBTTagCompound.get().cast(parsedNBT), "UUIDMost", "UUIDLeast", "WorldUUDMost", "WorldUUIDLeast", "Bukkit.updateLevel");
NMS.addToCompound(NBTTagCompound.get().cast(entNBT), NBTTagCompound.get().cast(parsedNBT));
NMS.setEntityNBT((Entity) tar, NBTTagCompound.get().cast(entNBT));
} else if (mode == ChangeMode.REMOVE) {
for (Object s : args) {
if (s != "UUIDMost" || s != "UUIDLeast" || s != "WorldUUIDMost" || s != "WorldUUIDLeast" || s != "Bukkit.updateLevel") {
NMS.removeFromCompound(NBTTagCompound.get().cast(entNBT), (String) s);
}
}
NMS.setEntityNBT((Entity) tar, NBTTagCompound.get().cast(entNBT));
}
} else if (tar instanceof Block) {
Object blockNBT = NBTTagCompound.get().cast(NMS.getTileNBT((Block) tar));
if (mode == ChangeMode.ADD) {
NMS.removeFromCompound(NBTTagCompound.get().cast(parsedNBT), "x", "y", "z", "id");
NMS.addToCompound(NBTTagCompound.get().cast(blockNBT), NBTTagCompound.get().cast(parsedNBT));
NMS.setTileNBT((Block) tar, NBTTagCompound.get().cast(blockNBT));
} else if (mode == ChangeMode.REMOVE) {
for (Object s : args) {
if (s != "x" || s != "y" || s != "z" || s != "id") {
NMS.removeFromCompound(NBTTagCompound.get().cast(blockNBT), (String) s);
}
}
NMS.setTileNBT((Block) tar, NBTTagCompound.get().cast(blockNBT));
}
} else if (tar instanceof ItemStack) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
Skript.warning("Failed to change the NBT of an item: Itemstack didn't have any slot attached to it.");
}
} else if (tar instanceof Slot) {
ItemStack slotItem = ((Slot) tar).getItem();
Object itemNBT = NBTTagCompound.get().cast(NMS.getItemNBT(slotItem));
if (mode == ChangeMode.ADD) {
NMS.addToCompound(NBTTagCompound.get().cast(itemNBT), NBTTagCompound.get().cast(parsedNBT));
ItemStack newItem = NMS.getItemWithNBT(slotItem, NBTTagCompound.get().cast(itemNBT));
((Slot) tar).setItem(newItem);
} else if (mode == ChangeMode.REMOVE) {
String[] toRemove = Arrays.copyOf(args, args.length, String[].class);
NMS.removeFromCompound(itemNBT, toRemove);
ItemStack newItem = NMS.getItemWithNBT(slotItem, NBTTagCompound.get().cast(itemNBT));
((Slot) tar).setItem(newItem);
} else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
ItemStack newItem = NMS.getItemWithNBT(slotItem, NBTTagCompound.get().cast(null));
((Slot) tar).setItem(newItem);
}
if (mode == ChangeMode.ADD) {
NMS.addTargetsNBT(tar, parsedNBT);
}else if (mode == ChangeMode.REMOVE) {
NMS.removeTargetsNBT(tar, args);
}else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
NMS.deleteTargetsNBT(tar);
}
}

Expand Down
19 changes: 0 additions & 19 deletions src/com/furkannm/skriptnms/util/nms/NBTBase.java

This file was deleted.

18 changes: 0 additions & 18 deletions src/com/furkannm/skriptnms/util/nms/NBTTagCompound.java

This file was deleted.

Loading

0 comments on commit f845bd6

Please sign in to comment.