Skip to content

Commit

Permalink
feat: (not tested) updated Takenaka to support 1.20.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Misat11 committed Apr 24, 2024
1 parent fe56e24 commit 1ba0676
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'org.screamingsandals.plugin-builder' version '1.0.84' apply false
id 'me.kcra.takenaka.accessor' version '1.1.0' apply false
id 'me.kcra.takenaka.accessor' version '1.1.2' apply false
id 'com.github.gmazzo.buildconfig' version '3.0.0' apply false
}

Expand Down
65 changes: 52 additions & 13 deletions nms/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.function.Supplier
apply plugin: 'me.kcra.takenaka.accessor'

dependencies {
mappingBundle "me.kcra.takenaka:mappings:1.8.8+1.20.4"
mappingBundle "me.kcra.takenaka:mappings:1.8.8+1.20.5"
api "me.kcra.takenaka:generator-accessor-runtime:${BuildConfig.BUILD_VERSION}"
}

Expand All @@ -15,7 +15,7 @@ dependencies {
accessors {
// uncomment this and remove the mappingBundle dependency,
// if you want to develop against custom versions
/*versionRange('1.8.8', '1.20.4') {
/*versionRange('1.8.8', '1.20.5') {
// exclude 1.16 and 1.10.1, they don't have most mappings and are basically not used at all
// exclude 1.8.9, client-only update - no Spigot mappings, no thank you
// exclude 1.9.1 and 1.9.3 - no mappings at all
Expand All @@ -38,9 +38,13 @@ accessors {
getter String, 'string'
}
var Attribute = mapClass 'net.minecraft.world.entity.ai.attributes.Attribute'
mapClass('net.minecraft.core.RegistryAccess') {
field 'net.minecraft.core.RegistryAccess$Frozen', 'EMPTY'
}
mapClass('net.minecraft.network.chat.Component$Serializer') {
method Component, 'fromJson', String // 1.16 and below
method 'net.minecraft.network.chat.MutableComponent', 'fromJson', String // 1.16.1 and higher
method 'net.minecraft.network.chat.MutableComponent', 'fromJsonLenient', String, 'net.minecraft.core.HolderLookup$Provider' // 1.20.5+
}
var SynchedEntityData = mapClass('net.minecraft.network.syncher.SynchedEntityData') {
method void, 'watch', int, Object
Expand Down Expand Up @@ -100,6 +104,7 @@ accessors {
}
var AttributeInstance = mapClass('net.minecraft.world.entity.ai.attributes.AttributeInstance') {
constructor Attribute, Consumer
constructor 'net.minecraft.core.Holder', Consumer // 1.20.5+
getter double, 'value'
setter double, 'baseValue'
}
Expand All @@ -109,6 +114,7 @@ accessors {
}
var LivingEntity = mapClass('net.minecraft.world.entity.LivingEntity') {
method AttributeInstance, 'getAttribute', Attribute
method AttributeInstance, 'getAttribute', 'net.minecraft.core.Holder'
getter AttributeMap, 'attributes'
getter 'net.minecraft.world.damagesource.CombatTracker', 'combatTracker'
}
Expand Down Expand Up @@ -142,17 +148,50 @@ accessors {
setter Entity, 'camera'
}
mapClass('net.minecraft.world.entity.ai.attributes.Attributes') {
field Attribute, 'MAX_HEALTH'
field Attribute, 'FOLLOW_RANGE'
field Attribute, 'KNOCKBACK_RESISTANCE'
field Attribute, 'MOVEMENT_SPEED'
field Attribute, 'FLYING_SPEED'
field Attribute, 'ATTACK_DAMAGE'
field Attribute, 'ATTACK_KNOCKBACK'
field Attribute, 'ATTACK_SPEED'
field Attribute, 'ARMOR'
field Attribute, 'ARMOR_TOUGHNESS'
field Attribute, 'LUCK'
fieldChain {
it.item Attribute, 'MAX_HEALTH'
it.item 'net.minecraft.core.Holder', 'MAX_HEALTH'
}
fieldChain {
it.item Attribute, 'FOLLOW_RANGE'
it.item 'net.minecraft.core.Holder', 'FOLLOW_RANGE'
}
fieldChain {
it.item Attribute, 'KNOCKBACK_RESISTANCE'
it.item 'net.minecraft.core.Holder', 'KNOCKBACK_RESISTANCE'
}
fieldChain {
it.item Attribute, 'MOVEMENT_SPEED'
it.item 'net.minecraft.core.Holder', 'MOVEMENT_SPEED'
}
fieldChain {
it.item Attribute, 'FLYING_SPEED'
it.item 'net.minecraft.core.Holder', 'FLYING_SPEED'
}
fieldChain {
it.item Attribute, 'ATTACK_DAMAGE'
it.item 'net.minecraft.core.Holder', 'ATTACK_DAMAGE'
}
fieldChain {
it.item Attribute, 'ATTACK_KNOCKBACK'
it.item 'net.minecraft.core.Holder', 'ATTACK_KNOCKBACK'
}
fieldChain {
it.item Attribute, 'ATTACK_SPEED'
it.item 'net.minecraft.core.Holder', 'ATTACK_SPEED'
}
fieldChain {
it.item Attribute, 'ARMOR'
it.item 'net.minecraft.core.Holder', 'ARMOR'
}
fieldChain {
it.item Attribute, 'ARMOR_TOUGHNESS'
it.item 'net.minecraft.core.Holder', 'ARMOR_TOUGHNESS'
}
fieldChain {
it.item Attribute, 'LUCK'
it.item 'net.minecraft.core.Holder', 'LUCK'
}
}
mapClass('net.minecraft.network.protocol.game.ServerboundInteractPacket') {
field int, 'entityId'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ public boolean hasAttribute(Attribute attribute) {

public boolean hasAttribute(Object attr) {
try {
Object attr0 = getMethod(handler, METHOD_GET_ATTRIBUTE.get())
.invoke(attr);
Object attr0;
if (METHOD_GET_ATTRIBUTE.get() != null) {
attr0 = getMethod(handler, METHOD_GET_ATTRIBUTE.get())
.invoke(attr);
} else {
attr0 = getMethod(handler, METHOD_GET_ATTRIBUTE_1.get())
.invoke(attr);
}
return attr0 != null;
} catch (Throwable t) {
}
Expand All @@ -70,8 +76,14 @@ public double getAttribute(Attribute attribute) {

public double getAttribute(Object attr) {
try {
Object attr0 = getMethod(handler, METHOD_GET_ATTRIBUTE.get())
.invoke(attr);
Object attr0;
if (METHOD_GET_ATTRIBUTE.get() != null) {
attr0 = getMethod(handler, METHOD_GET_ATTRIBUTE.get())
.invoke(attr);
} else {
attr0 = getMethod(handler, METHOD_GET_ATTRIBUTE_1.get())
.invoke(attr);
}
return (double) getMethod(attr0, AttributeInstanceAccessor.METHOD_GET_VALUE.get()).invoke();
} catch (Throwable t) {
}
Expand All @@ -85,8 +97,14 @@ public void setAttribute(Attribute attribute, double value) {
public void setAttribute(Object attr, double value) {
try {
if (value >= 0) {
Object attr0 = getMethod(handler, METHOD_GET_ATTRIBUTE.get())
.invoke(attr);
Object attr0;
if (METHOD_GET_ATTRIBUTE.get() != null) {
attr0 = getMethod(handler, METHOD_GET_ATTRIBUTE.get())
.invoke(attr);
} else {
attr0 = getMethod(handler, METHOD_GET_ATTRIBUTE_1.get())
.invoke(attr);
}
if (attr0 == null) {
Object attrMap = getMethod(handler, METHOD_GET_ATTRIBUTES.get()).invoke();
// Pre 1.16
Expand All @@ -96,9 +114,15 @@ public void setAttribute(Object attr, double value) {
Object provider = getField(attrMap, AttributeMapAccessor.FIELD_SUPPLIER.get());
Map<Object, Object> all = Maps
.newHashMap((Map<?, ?>) getField(provider, AttributeSupplierAccessor.FIELD_INSTANCES.get()));
attr0 = AttributeInstanceAccessor.CONSTRUCTOR_0.get().newInstance(attr, (Consumer) o -> {
// do nothing
});
if (AttributeInstanceAccessor.CONSTRUCTOR_0.get() != null) {
attr0 = AttributeInstanceAccessor.CONSTRUCTOR_0.get().newInstance(attr, (Consumer) o -> {
// do nothing
});
} else {
attr0 = AttributeInstanceAccessor.CONSTRUCTOR_1.get().newInstance(attr, (Consumer) o -> {
// do nothing
});
}
all.put(attr, attr0);
setField(provider, AttributeSupplierAccessor.FIELD_INSTANCES.get(), ImmutableMap.copyOf(all));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ public Object getDataWatcher() {
public void setCustomName(String name) {
InstanceMethod method = ClassStorage.getMethod(handler, METHOD_SET_CUSTOM_NAME.get());
if (method.getReflectedMethod() != null) {
method.invoke(ClassStorage.getMethod(TabManager.getCorrectSerializingMethod())
.invokeStatic("{\"text\": \"" + name + "\"}"));
method.invoke(TabManager.serialize("{\"text\": \"" + name + "\"}"));
} else {
ClassStorage.getMethod(handler, METHOD_SET_CUSTOM_NAME_1.get()).invoke(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public EntityTextDisplayNMS(Location loc) throws Throwable {

public void setText(String name) {
InstanceMethod method = ClassStorage.getMethod(handler, METHOD_SET_TEXT.get());
method.invoke(ClassStorage.getMethod(TabManager.getCorrectSerializingMethod()).invokeStatic("{\"text\": \"" + name + "\"}"));
method.invoke(TabManager.serialize("{\"text\": \"" + name + "\"}"));
}

public String getText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.screamingsandals.bedwars.game.GamePlayer;
import org.screamingsandals.bedwars.lib.nms.accessors.ClientboundTabListPacketAccessor;
import org.screamingsandals.bedwars.lib.nms.accessors.Component$SerializerAccessor;
import org.screamingsandals.bedwars.lib.nms.accessors.RegistryAccessAccessor;
import org.screamingsandals.bedwars.lib.nms.utils.ClassStorage;

import java.lang.reflect.Method;
import java.util.ArrayList;
Expand Down Expand Up @@ -54,20 +56,16 @@ public void modifyForPlayer(GamePlayer player) {
try {
Object headerComponent;
if (header != null) {
headerComponent = getMethod(getCorrectSerializingMethod())
.invokeStatic("{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', String.join("\n", translate(player, header))) + "\"}");
headerComponent = serialize("{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', String.join("\n", translate(player, header))) + "\"}");
} else {
headerComponent = getMethod(getCorrectSerializingMethod())
.invokeStatic("{\"text\": \"\"}");
headerComponent = serialize("{\"text\": \"\"}");
}

Object footerComponent;
if (footer != null) {
footerComponent = getMethod(getCorrectSerializingMethod())
.invokeStatic("{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', String.join("\n", translate(player, footer))) + "\"}");
footerComponent = serialize("{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', String.join("\n", translate(player, footer))) + "\"}");
} else {
footerComponent = getMethod(getCorrectSerializingMethod())
.invokeStatic("{\"text\": \"\"}");
footerComponent = serialize("{\"text\": \"\"}");
}

Object packet;
Expand All @@ -94,8 +92,7 @@ public void clear(GamePlayer player) {
} else {
clearString = "{\"translate\": \"\"}";
}
Object blankComponent = getMethod(getCorrectSerializingMethod())
.invokeStatic(clearString);
Object blankComponent = serialize(clearString);
Object packet;
if (ClientboundTabListPacketAccessor.CONSTRUCTOR_1.get() != null) {
packet = ClientboundTabListPacketAccessor.CONSTRUCTOR_1.get().newInstance(blankComponent, blankComponent);
Expand All @@ -122,10 +119,13 @@ public List<String> translate(GamePlayer gamePlayer, List<String> origin) {
return list;
}

public static Method getCorrectSerializingMethod() {
public static Object serialize(String text) {
if (Component$SerializerAccessor.METHOD_FROM_JSON.get() != null) {
return Component$SerializerAccessor.METHOD_FROM_JSON.get();
return ClassStorage.getMethod(Component$SerializerAccessor.METHOD_FROM_JSON.get()).invokeStatic(text);
}
return Component$SerializerAccessor.METHOD_FROM_JSON_1.get();
if (Component$SerializerAccessor.METHOD_FROM_JSON_1.get() != null) {
return ClassStorage.getMethod(Component$SerializerAccessor.METHOD_FROM_JSON_1.get()).invokeStatic(text);
}
return ClassStorage.getMethod(Component$SerializerAccessor.METHOD_FROM_JSON_LENIENT.get()).invokeStatic(text, RegistryAccessAccessor.FIELD_EMPTY.get());
}
}

0 comments on commit 1ba0676

Please sign in to comment.