Skip to content

Commit

Permalink
Add 1.20.2 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
gmitch215 committed Sep 23, 2023
1 parent 3d1fff9 commit 4ec28c6
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 2 deletions.
39 changes: 39 additions & 0 deletions nms/1_20_R2/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import io.github.patrick.gradle.remapper.RemapTask

plugins {
id("io.github.patrick.remapper") version "1.4.0"
}

val mcVersion = "1.20.2"

dependencies {
api(project(":novaconomy-abstract"))
api(project(":novaconomy-api"))

compileOnly("org.spigotmc:spigot:$mcVersion-R0.1-SNAPSHOT:remapped-mojang")
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

tasks {
assemble {
dependsOn("remap")
}

jar.configure {
enabled = false
}

remap {
dependsOn("shadowJar")

inputTask.set(getByName<ShadowJar>("shadowJar"))
version.set(mcVersion)
action.set(RemapTask.Action.MOJANG_TO_SPIGOT)
archiveName.set("${project.name}-${project.version}.jar")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package us.teaminceptus.novaconomy.v1_20_R2;

import net.minecraft.nbt.CompoundTag;
import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import us.teaminceptus.novaconomy.abstraction.NBTWrapper;
import us.teaminceptus.novaconomy.api.business.Business;
import us.teaminceptus.novaconomy.api.economy.Economy;
import us.teaminceptus.novaconomy.api.util.BusinessProduct;
import us.teaminceptus.novaconomy.api.util.Product;

import java.util.UUID;

import static us.teaminceptus.novaconomy.abstraction.Wrapper.ROOT;

final class NBTWrapper1_20_R2 extends NBTWrapper {

public NBTWrapper1_20_R2(ItemStack item) {
super(item);
}

@Override
public String getString(String key) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

return novaconomy.getString(key);
}

@Override
public void set(String key, String value) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

novaconomy.putString(key, value);
tag.put(ROOT, novaconomy);
nmsitem.setTag(tag);
item = CraftItemStack.asBukkitCopy(nmsitem);
}

@Override
public int getInt(String key) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

return novaconomy.getInt(key);
}

@Override
public void set(String key, int value) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

novaconomy.putInt(key, value);
tag.put(ROOT, novaconomy);
nmsitem.setTag(tag);
item = CraftItemStack.asBukkitCopy(nmsitem);
}

@Override
public UUID getUUID(String key) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

return novaconomy.getUUID(key);
}

@Override
public void set(String key, UUID id) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

novaconomy.putUUID(key, id);
tag.put(ROOT, novaconomy);
nmsitem.setTag(tag);
item = CraftItemStack.asBukkitCopy(nmsitem);
}

@Override
public double getDouble(String key) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

return novaconomy.getDouble(key);
}

@Override
public void set(String key, double value) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

novaconomy.putDouble(key, value);
tag.put(ROOT, novaconomy);
nmsitem.setTag(tag);

item = CraftItemStack.asBukkitCopy(nmsitem);
}

@Override
public boolean getBoolean(String key) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

return novaconomy.getBoolean(key);
}

@Override
public void set(String key, boolean value) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

novaconomy.putBoolean(key, value);
tag.put(ROOT, novaconomy);
nmsitem.setTag(tag);
item = CraftItemStack.asBukkitCopy(nmsitem);
}

@Override
public Product getProduct(String key) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

CompoundTag productT = novaconomy.getCompound(key);
if (productT.isEmpty()) return null;

double amount = productT.getDouble("amount");
Economy econ = Economy.getEconomy(productT.getUUID("economy"));
ItemStack item = CraftItemStack.asBukkitCopy(net.minecraft.world.item.ItemStack.of(productT.getCompound("item")));

Product p = new Product(item, econ, amount);

if (productT.contains("business")) {
Business b = Business.byId(productT.getUUID("business"));
return new BusinessProduct(p, b);
}

return p;
}

@Override
public void set(String key, Product product) {
net.minecraft.world.item.ItemStack nmsitem = CraftItemStack.asNMSCopy(item);
CompoundTag tag = nmsitem.getOrCreateTag();
CompoundTag novaconomy = tag.getCompound(ROOT);

CompoundTag productT = new CompoundTag();
productT.putDouble("amount", product.getAmount());
productT.putUUID("economy", product.getEconomy().getUniqueId());
productT.put("item", CraftItemStack.asNMSCopy(product.getItem()).save(CraftItemStack.asNMSCopy(product.getItem()).getOrCreateTag()));

if (product instanceof BusinessProduct bp)
productT.putUUID("business", bp.getBusiness().getUniqueId());

novaconomy.put(key, productT);
tag.put(ROOT, novaconomy);
nmsitem.setTag(tag);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package us.teaminceptus.novaconomy.v1_20_R2;

import com.google.common.collect.ImmutableMap;
import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftInventoryCustom;
import us.teaminceptus.novaconomy.abstraction.NovaInventory;

import java.util.HashMap;
import java.util.Map;

final class NovaInventory1_20_R2 extends CraftInventoryCustom implements NovaInventory{

private final Map<String, Object> attributes = new HashMap<>();

public NovaInventory1_20_R2(String id, String name, int size) {
super(null, size, name);

setAttribute("_id", id);
setAttribute("_name", name);
}

@Override
public Map<String, Object> getAttribtes() {
return ImmutableMap.copyOf(attributes);
}

@Override
public void setAttribute(String key, Object value) {
attributes.put(key, value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package us.teaminceptus.novaconomy.v1_20_R2;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.network.protocol.Packet;
import org.bukkit.entity.Player;
import us.teaminceptus.novaconomy.util.NovaUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Predicate;

final class PacketHandler1_20_R2 extends ChannelDuplexHandler {

public static final Map<UUID, Predicate<Packet<?>>> PACKET_HANDLERS = new HashMap<>();

private final Player p;

public PacketHandler1_20_R2(Player p) {
this.p = p;
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object packetO) throws Exception {
if (!(packetO instanceof Packet<?> packet)) {
super.channelRead(ctx, packetO);
return;
}

Predicate<Packet<?>> handler = PACKET_HANDLERS.get(p.getUniqueId());
if (handler != null) NovaUtil.sync(() -> {
boolean success = handler.test(packet);
if (success) PACKET_HANDLERS.remove(p.getUniqueId());
});

super.channelRead(ctx, packetO);
}

}
Loading

0 comments on commit 4ec28c6

Please sign in to comment.