Skip to content

Commit

Permalink
Merge pull request #35 from SourceWriters/development
Browse files Browse the repository at this point in the history
Fixed textures not working for Head blocks
  • Loading branch information
Lauriichan authored Jan 23, 2022
2 parents 6180e13 + d6e1444 commit e025804
Show file tree
Hide file tree
Showing 56 changed files with 1,057 additions and 718 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.sourcewriters.minecraft</groupId>
<artifactId>vcompat-parent</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
<packaging>pom</packaging>

<distributionManagement>
Expand Down
2 changes: 1 addition & 1 deletion vcompat-1_17_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.sourcewriters.minecraft</groupId>
<artifactId>vcompat-parent</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
</parent>
<artifactId>vcompat-1_17_R1</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftSkull;
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
Expand All @@ -15,25 +16,32 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.SkullBlockEntity;
import net.sourcewriters.minecraft.vcompat.provider.TextureProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup;

public class TextureProvider1_17_R1 extends TextureProvider<VersionControl1_17_R1> {

private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity");
private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle");
private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_17_R1.inventory.CraftMetaSkull")
.searchField("serialized", "serializedProfile").searchField("profile", "profile");
private final ClassLookup craftEntityStateRef;
private final ClassLookup craftItemStackRef;
private final ClassLookup craftMetaSkullRef;

protected TextureProvider1_17_R1(VersionControl1_17_R1 versionControl) {
super(versionControl);
ClassLookupProvider provider = versionControl.getLookupProvider();
craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity",
"tileEntity");
craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle");
craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull")
.searchField("serialized", "serializedProfile").searchField("profile", "profile");
}

@Override
public GameProfile profileFromBlock(Block block) {
if (!(block instanceof CraftSkull)) {
BlockState state = block.getState();
if (!(state instanceof CraftSkull)) {
return null;
}
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity");
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity");
return entitySkull.owner;
}

Expand Down Expand Up @@ -90,10 +98,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p

@Override
public boolean applyBlock(Block block, GameProfile profile) {
if (!(block instanceof CraftSkull)) {
BlockState state = block.getState();
if (!(state instanceof CraftSkull)) {
return false;
}
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity");
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity");
entitySkull.setOwner(profile);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.sourcewriters.minecraft.vcompat.provider.data.WrappedContainer;
import net.sourcewriters.minecraft.vcompat.provider.impl.v1_17_R1.data.BukkitContainer1_17_R1;
import net.sourcewriters.minecraft.vcompat.provider.impl.v1_17_R1.data.SyntaxContainer1_17_R1;
import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup;
import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.nbt.NbtCompound;

Expand All @@ -29,13 +30,16 @@ public final class BukkitContainerAdapterHook1_17_R1 {

private static final BukkitContainerAdapterHook1_17_R1 HOOK = new BukkitContainerAdapterHook1_17_R1();

private final ClassLookup registryRef = ClassLookup.of(CraftPersistentDataTypeRegistry.class)
.searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class)
.searchField("adapters", "adapters")
.searchField("function", "CREATE_ADAPTER");
private final ClassLookup entityRef = ClassLookup.of(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY");
private final ClassLookup registryRef;
private final ClassLookup entityRef;

private BukkitContainerAdapterHook1_17_R1() {}
private BukkitContainerAdapterHook1_17_R1() {
ClassLookupProvider provider = VersionCompatProvider.get().getLookupProvider();
registryRef = provider.createLookup("CraftPersistentDataTypeRegistry", CraftPersistentDataTypeRegistry.class)
.searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class)
.searchField("adapters", "adapters").searchField("function", "CREATE_ADAPTER");
entityRef = provider.createLookup("CraftEntity", CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY");
}

private final HashMap<CraftPersistentDataTypeRegistry, Function> map = new HashMap<>();

Expand All @@ -58,7 +62,7 @@ private void inject(CraftPersistentDataTypeRegistry registry) {
return;
}
map.put(registry, (Function) registryRef.getFieldValue(registry, "function"));
Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").type().returnType(), (Class) clazz);
Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz);
registryRef.setFieldValue(registry, "function", function);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
package net.sourcewriters.minecraft.vcompat.provider.impl.v1_17_R1.tools;

import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftSkull;

import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;

import net.minecraft.world.level.block.entity.SkullBlockEntity;
import net.sourcewriters.minecraft.vcompat.VersionCompatProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup;
import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools;
import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants;

public class BlockTools1_17_R1 extends BlockTools {

private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity");
private final ClassLookup craftEntityStateRef;

public BlockTools1_17_R1() {
craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class)
.searchField("tileEntity", "tileEntity");
}

@Override
public void setHeadTexture(Block block, String texture) {
if (!(block instanceof CraftSkull)) {
BlockState state = block.getState();
if (!(state instanceof CraftSkull)) {
return;
}
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity");
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity");
PropertyMap map = entitySkull.owner.getProperties();
map.removeAll("textures");
map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture));
}

@Override
public String getHeadTexture(Block block) {
if (!(block instanceof CraftSkull)) {
BlockState state = block.getState();
if (!(state instanceof CraftSkull)) {
return null;
}
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity");
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity");
return entitySkull.owner.getProperties().get("textures").iterator().next().getValue();
}

Expand Down
2 changes: 1 addition & 1 deletion vcompat-1_18_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.sourcewriters.minecraft</groupId>
<artifactId>vcompat-parent</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
</parent>
<artifactId>vcompat-1_18_R1</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.v1_18_R1.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.v1_18_R1.block.CraftSkull;
import org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack;
Expand All @@ -15,25 +16,33 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.SkullBlockEntity;
import net.sourcewriters.minecraft.vcompat.provider.TextureProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup;

public class TextureProvider1_18_R1 extends TextureProvider<VersionControl1_18_R1> {

private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftBlockEntityState.class).searchField("tileEntity", "tileEntity");
private final ClassLookup craftItemStackRef = ClassLookup.of(CraftItemStack.class).searchField("handle", "handle");
private final ClassLookup craftMetaSkullRef = ClassLookup.of("org.bukkit.craftbukkit.v1_18_R1.inventory.CraftMetaSkull")
.searchField("serialized", "serializedProfile").searchField("profile", "profile");
private final ClassLookup craftEntityStateRef;
private final ClassLookup craftItemStackRef;
private final ClassLookup craftMetaSkullRef;

protected TextureProvider1_18_R1(VersionControl1_18_R1 versionControl) {
super(versionControl);
ClassLookupProvider provider = versionControl.getLookupProvider();
craftEntityStateRef = provider.createLookup("CraftBlockEntityState", CraftBlockEntityState.class).searchField("tileEntity",
"tileEntity");
craftItemStackRef = provider.createLookup("CraftItemStack", CraftItemStack.class).searchField("handle", "handle");
craftMetaSkullRef = provider.createCBLookup("CraftMetaSkull", "inventory.CraftMetaSkull")
.searchField("serialized", "serializedProfile").searchField("profile", "profile");

}

@Override
public GameProfile profileFromBlock(Block block) {
if (!(block instanceof CraftSkull)) {
BlockState state = block.getState();
if (!(state instanceof CraftSkull)) {
return null;
}
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity");
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity");
return entitySkull.owner;
}

Expand Down Expand Up @@ -90,10 +99,11 @@ public boolean applyItem(org.bukkit.inventory.ItemStack itemStack, GameProfile p

@Override
public boolean applyBlock(Block block, GameProfile profile) {
if (!(block instanceof CraftSkull)) {
BlockState state = block.getState();
if (!(state instanceof CraftSkull)) {
return false;
}
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity");
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity");
entitySkull.setOwner(profile);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.sourcewriters.minecraft.vcompat.provider.data.WrappedContainer;
import net.sourcewriters.minecraft.vcompat.provider.impl.v1_18_R1.data.BukkitContainer1_18_R1;
import net.sourcewriters.minecraft.vcompat.provider.impl.v1_18_R1.data.SyntaxContainer1_18_R1;
import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup;
import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.nbt.NbtCompound;

Expand All @@ -29,13 +30,16 @@ public final class BukkitContainerAdapterHook1_18_R1 {

private static final BukkitContainerAdapterHook1_18_R1 HOOK = new BukkitContainerAdapterHook1_18_R1();

private final ClassLookup registryRef = ClassLookup.of(CraftPersistentDataTypeRegistry.class)
.searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class)
.searchField("adapters", "adapters")
.searchField("function", "CREATE_ADAPTER");
private final ClassLookup entityRef = ClassLookup.of(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY");
private final ClassLookup registryRef;
private final ClassLookup entityRef;

private BukkitContainerAdapterHook1_18_R1() {}
private BukkitContainerAdapterHook1_18_R1() {
ClassLookupProvider provider = VersionCompatProvider.get().getLookupProvider();
registryRef = provider.createLookup("CraftPersistentDataTypeRegistry", CraftPersistentDataTypeRegistry.class)
.searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class)
.searchField("adapters", "adapters").searchField("function", "CREATE_ADAPTER");
entityRef = provider.createLookup("CraftEntity", CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY");
}

private final HashMap<CraftPersistentDataTypeRegistry, Function> map = new HashMap<>();

Expand All @@ -58,7 +62,7 @@ private void inject(CraftPersistentDataTypeRegistry registry) {
return;
}
map.put(registry, (Function) registryRef.getFieldValue(registry, "function"));
Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").type().returnType(), (Class) clazz);
Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz);
registryRef.setFieldValue(registry, "function", function);
}

Expand All @@ -73,8 +77,7 @@ private <E> E createAdapter(CraftPersistentDataTypeRegistry registry, Class<E> a
}

private <C extends WrappedContainer> Object buildAdapter(Object handle, Class<C> type, Function<CompoundTag, C> function) {
return registryRef.run(handle, "create", type, CompoundTag.class, (Function<C, CompoundTag>) input -> toPrimitive(input),
function);
return registryRef.run(handle, "create", type, CompoundTag.class, (Function<C, CompoundTag>) input -> toPrimitive(input), function);
}

private CompoundTag toPrimitive(WrappedContainer input) {
Expand All @@ -88,7 +91,8 @@ private CompoundTag toPrimitive(WrappedContainer input) {
}
if (handle instanceof IDataContainer) {
if (handle instanceof NbtContainer) {
return (CompoundTag) VersionCompatProvider.get().getControl().getBukkitConversion().toMinecraftCompound(((NbtContainer) handle).asNbt());
return (CompoundTag) VersionCompatProvider.get().getControl().getBukkitConversion()
.toMinecraftCompound(((NbtContainer) handle).asNbt());
}
throw new IllegalArgumentException(
"Expected 'CraftPersistentDataContainer' got '" + handle.getClass().getSimpleName() + " instead'!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
package net.sourcewriters.minecraft.vcompat.provider.impl.v1_18_R1.tools;

import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.v1_18_R1.block.CraftSkull;

import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;

import net.minecraft.world.level.block.entity.SkullBlockEntity;
import net.sourcewriters.minecraft.vcompat.VersionCompatProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup;
import net.sourcewriters.minecraft.vcompat.provider.tools.BlockTools;
import net.sourcewriters.minecraft.vcompat.util.constants.MinecraftConstants;

public class BlockTools1_18_R1 extends BlockTools {

private final ClassLookup craftEntityStateRef = ClassLookup.of(CraftSkull.class).searchField("tileEntity", "tileEntity");
private final ClassLookup craftEntityStateRef;

public BlockTools1_18_R1() {
craftEntityStateRef = VersionCompatProvider.get().getLookupProvider().createLookup("CraftSkull", CraftSkull.class)
.searchField("tileEntity", "tileEntity");
}

@Override
public void setHeadTexture(Block block, String texture) {
if (!(block instanceof CraftSkull)) {
BlockState state = block.getState();
if (!(state instanceof CraftSkull)) {
return;
}
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity");
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity");
PropertyMap map = entitySkull.owner.getProperties();
map.removeAll("textures");
map.put("textures", new Property("textures", MinecraftConstants.TEXTURE_SIGNATURE, texture));
}

@Override
public String getHeadTexture(Block block) {
if (!(block instanceof CraftSkull)) {
BlockState state = block.getState();
if (!(state instanceof CraftSkull)) {
return null;
}
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(block, "tileEntity");
SkullBlockEntity entitySkull = (SkullBlockEntity) craftEntityStateRef.getFieldValue(state, "tileEntity");
return entitySkull.owner.getProperties().get("textures").iterator().next().getValue();
}

Expand Down
2 changes: 1 addition & 1 deletion vcompat-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>net.sourcewriters.minecraft</groupId>
<artifactId>vcompat-parent</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
</parent>
<artifactId>vcompat-api</artifactId>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package net.sourcewriters.minecraft.vcompat.provider;

import net.sourcewriters.minecraft.vcompat.VersionCompatProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider;

public abstract class VersionControl {

protected final DataProvider dataProvider = new DataProvider(this);
protected final ClassLookupProvider lookupProvider = VersionCompatProvider.get().getLookupProvider();

public DataProvider getDataProvider() {
return dataProvider;
}

public final ClassLookupProvider getLookupProvider() {
return lookupProvider;
}

public abstract ToolProvider<?> getToolProvider();

Expand Down
Loading

0 comments on commit e025804

Please sign in to comment.