Skip to content

Commit

Permalink
Addon's Files
Browse files Browse the repository at this point in the history
  • Loading branch information
Furkan Mercimek committed Sep 16, 2018
1 parent c74d2c9 commit 66796d9
Show file tree
Hide file tree
Showing 27 changed files with 1,110 additions and 0 deletions.
6 changes: 6 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: skript-nms
author: FurkannM
description: A Skript addon for NMS stuff
version: 0.0.1
main: com.furkannm.skriptnms.Core
softdepend: [Skript]
33 changes: 33 additions & 0 deletions src/com/furkannm/skriptnms/Core.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.furkannm.skriptnms;

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.effects.EffLoadDat;
import com.furkannm.skriptnms.expressions.ExprFileNbt;
import com.furkannm.skriptnms.expressions.ExprNBTOf;

public class Core extends JavaPlugin{
static String ver;

@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);
}
}

public static String getVer() {
return ver;
}
}
54 changes: 54 additions & 0 deletions src/com/furkannm/skriptnms/effects/EffLoadDat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.furkannm.skriptnms.effects;

import java.io.File;

import javax.annotation.Nullable;

import org.bukkit.event.Event;

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

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

@Name("Load Dat File")
@Examples({
"load nbt from \"world/level.dat\""
})

public class EffLoadDat extends Effect {

//load dat file in %string%
private File lastLoadedFile = null;
private Expression<String> fileName;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean kleen, ParseResult result) {
fileName = (Expression<String>) expr[0];
return true;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "the loaded NBT from file " + lastLoadedFile.toString();
}

@Override
protected void execute(Event e) {
String f = fileName.getSingle(e);
f = f.endsWith(".dat") ? f : f + ".dat";
lastLoadedFile = new File(f);
if(lastLoadedFile.exists()) {
NMS.loadFileNbt(lastLoadedFile);
}else{
Skript.warning("Error when loading dat file");
}
}

}
119 changes: 119 additions & 0 deletions src/com/furkannm/skriptnms/expressions/ExprFileNbt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.furkannm.skriptnms.expressions;

import java.io.File;

import javax.annotation.Nullable;

import org.bukkit.event.Event;

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.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.NMS;
import com.furkannm.skriptnms.util.nms.types.DatFile;

@Name("File NBT")
@Examples({
"nbt from \"world/level.dat\"",
"last loaded file's nbt"
})

public class ExprFileNbt extends SimpleExpression<Object>{

private Expression<Object> target;

@SuppressWarnings("unchecked")
@Override
public Class<? extends Object> getReturnType() {
return NBTTagCompound.get();
}

@Override
public boolean isSingle() {
return true;
}

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult result) {
if(expr != null && expr.length>0) {
target = (Expression<Object>) expr[0];
}else{
target = null;
}
return true;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
if(target != null) {
return "the NBT from file " + target.toString(e, debug);
}else{
if(DatFile.getFile() != null) {
return "the NBT from file " + DatFile.getFile().getName();
}else{
return null;
}
}
}

@Override
@Nullable
protected Object[] get(Event e) {
if (target != null) {
Object file = target.getSingle(e);
file = file.toString().endsWith(".dat") ? file.toString() : file.toString() + ".dat";
File f = new File(file.toString());
if(!f.exists()) return null;
NMS.loadFileNbt(f);
return new Object[] { NBTTagCompound.get().cast(DatFile.getNbt()) };
}else{
return new Object[] { NBTTagCompound.get().cast(DatFile.getNbt()) };
}
}

@Override
public void change(Event e, Object[] args, ChangeMode mode) {
File file;
if(target != null) {
Object f = target.getSingle(e);
f = f.toString().endsWith(".dat") ? f.toString() : f.toString() + ".dat";
file = new File(f.toString());

}else{
file = DatFile.getFile();
}
Object parsedNBT = null;
if (args != null) {
parsedNBT = NBTTagCompound.get().cast(NMS.parseRawNBT(((String) args[0])));
}

Object fileNBT = NBTTagCompound.get().cast(DatFile.getNbt());
if (mode == ChangeMode.ADD) {
NMS.addToCompound(NBTTagCompound.get().cast(fileNBT), NBTTagCompound.get().cast(parsedNBT));
NMS.setFileNBT(file, fileNBT);
} else if (mode == ChangeMode.REMOVE) {
for (Object s : args) {
NMS.removeFromCompound(NBTTagCompound.get().cast(fileNBT), (String) s);
}
NMS.setFileNBT(file, fileNBT);
}

}

@Override
public Class<?>[] acceptChange(final ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) {
return CollectionUtils.array(String[].class);
}
return null;
}

}
146 changes: 146 additions & 0 deletions src/com/furkannm/skriptnms/expressions/ExprNBTOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.furkannm.skriptnms.expressions;

import java.util.Arrays;

import javax.annotation.Nullable;

import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;

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.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;

@Name("NBT of")
@Examples({
"on place of furnace:",
"\tadd \"{BurnTime:100s}\" to nbt of event-block"
})

public class ExprNBTOf extends SimpleExpression<Object> {

private Expression<Object> target;

@SuppressWarnings("unchecked")
@Override
public Class<? extends Object> getReturnType() {
return NBTTagCompound.get();
}

@Override
public boolean isSingle() {
return true;
}

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean arg2, ParseResult result) {
target = (Expression<Object>) expr[0];
Class<?> type = target.getReturnType();
if (type != Entity.class || type != Block.class || type != ItemStack.class || type != Slot.class) {
Skript.error(target.toString() + " is not entity, block or itemstack.", ErrorQuality.SEMANTIC_ERROR);
}
return true;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return target.toString(e, debug) + "'s NBT value";
}

@Override
@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;
}

@Override
public void change(Event e, Object[] args, ChangeMode mode) {
Object tar = target.getSingle(e);
Object parsedNBT = null;
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") { // Prevent crucial data from being modified
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);
}
}
}

@Override
public Class<?>[] acceptChange(final ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) {
return CollectionUtils.array(String[].class);
}
return null;
}

}
19 changes: 19 additions & 0 deletions src/com/furkannm/skriptnms/util/nms/NBTBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.furkannm.skriptnms.util.nms;


import com.furkannm.skriptnms.Core;

public class NBTBase {

@SuppressWarnings("rawtypes")
public static Class get() {
Class NbtBase = null;
try {
NbtBase = Class.forName("net.minecraft.server."+Core.getVer()+".NBTBase");
} catch (SecurityException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return NbtBase;
}
}
Loading

0 comments on commit 66796d9

Please sign in to comment.