Skip to content

Commit

Permalink
1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
manyrandomthings committed Jun 8, 2022
1 parent 658f0e9 commit 844df95
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 77 deletions.
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '0.10-SNAPSHOT'
id 'fabric-loom' version '0.12-SNAPSHOT'
id 'maven-publish'
}

Expand All @@ -26,6 +26,11 @@ dependencies {

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// Uncomment the following line to enable the deprecated Fabric API modules.
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.

// modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}"
}

processResources {
Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version=1.18
yarn_mappings=1.18+build.1
loader_version=0.12.6
# check these on https://fabricmc.net/develop
minecraft_version=1.19
yarn_mappings=1.19+build.1
loader_version=0.14.6

# Mod Properties
mod_version = 1.1.0
mod_version = 1.1.1
maven_group = head-name-fix
archives_base_name = head-name-fix

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.43.1+1.18
fabric_version=0.55.2+1.19
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
12 changes: 6 additions & 6 deletions src/main/java/headfix/HeadFix.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package headfix;

import java.util.HashSet;
import java.util.Set;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.loot.v1.event.LootTableLoadingCallback;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;
import net.minecraft.block.Blocks;
import net.minecraft.loot.function.CopyNameLootFunction;
import net.minecraft.util.Identifier;

import java.util.HashSet;
import java.util.Set;

public class HeadFix implements ModInitializer {
public static final Set<Identifier> HEAD_LOOT_TABLES = new HashSet<>(Set.of(
Blocks.CREEPER_HEAD.getLootTableId(),
Expand All @@ -21,11 +21,11 @@ public class HeadFix implements ModInitializer {

@Override
public void onInitialize() {
LootTableLoadingCallback.EVENT.register((resourceManager, manager, id, supplier, setter) -> {
LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> {
// check if head loot table
if(HEAD_LOOT_TABLES.contains(id)) {
// add copy name function
supplier.withFunction(CopyNameLootFunction.builder(CopyNameLootFunction.Source.BLOCK_ENTITY).build());
tableBuilder.apply(CopyNameLootFunction.builder(CopyNameLootFunction.Source.BLOCK_ENTITY).build());
}
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/headfix/SetableNameable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package headfix;

import net.minecraft.text.Text;
import net.minecraft.util.Nameable;

public interface SetableNameable extends Nameable {
// Allows customName to be set
void setCustomName(Text customName);
}
10 changes: 0 additions & 10 deletions src/main/java/headfix/SkullBlockEntityAccessor.java

This file was deleted.

37 changes: 8 additions & 29 deletions src/main/java/headfix/mixins/AbstractSkullBlockMixin.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,26 @@
package headfix.mixins;

import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import headfix.SkullBlockEntityAccessor;
import headfix.SetableNameable;
import net.minecraft.block.AbstractSkullBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.SkullBlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(AbstractSkullBlock.class)
public abstract class AbstractSkullBlockMixin extends BlockWithEntity {
protected AbstractSkullBlockMixin(Settings settings) {
super(settings);
}

public abstract class AbstractSkullBlockMixin extends BlockMixin {
@Override
@Intrinsic
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
super.onPlaced(world, pos, state, placer, itemStack);
}

@Inject(
method = "onPlaced(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/entity/LivingEntity;Lnet/minecraft/item/ItemStack;)V",
at = @At("TAIL")
)
private void addNameToBlockEntity(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack, CallbackInfo ci) {
protected void onPlacedTailInject(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack, CallbackInfo ci) {
// check if item has a name when being placed
if(!world.isClient && itemStack.hasCustomName()) {
// get block entity at placed location
BlockEntity blockEntity = world.getBlockEntity(pos);
// check if block entity is a head block entity
if(blockEntity instanceof SkullBlockEntity) {
// get block entity at placed location and check if block entity is a head block entity
if(world.getBlockEntity(pos) instanceof SkullBlockEntity skullBlockEntity) {
// set custom name of head to the name of the item being placed
((SkullBlockEntityAccessor) blockEntity).setCustomName(itemStack.getName());
((SetableNameable) skullBlockEntity).setCustomName(itemStack.getName());
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/headfix/mixins/BlockMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package headfix.mixins;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Block.class)
public class BlockMixin {
@Inject(method = "onPlaced", at = @At("TAIL"))
protected void onPlacedTailInject(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack, CallbackInfo ci) {
// blank
}
}
15 changes: 7 additions & 8 deletions src/main/java/headfix/mixins/DispenserBehaviorMixin.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package headfix.mixins;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import headfix.SkullBlockEntityAccessor;
import headfix.SetableNameable;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPointer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(targets = "net/minecraft/block/dispenser/DispenserBehavior$15")
public abstract class DispenserBehaviorMixin {
Expand All @@ -26,7 +25,7 @@ public abstract class DispenserBehaviorMixin {
)
private void addNameToDispensed(BlockPointer pointer, ItemStack stack, CallbackInfoReturnable<ItemStack> cir, World world, Direction direction, BlockPos blockPos, BlockEntity blockEntity) {
if(stack.hasCustomName()) {
((SkullBlockEntityAccessor) blockEntity).setCustomName(stack.getName());
((SetableNameable) blockEntity).setCustomName(stack.getName());
}
}
}
36 changes: 22 additions & 14 deletions src/main/java/headfix/mixins/SkullBlockEntityMixin.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package headfix.mixins;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import headfix.SkullBlockEntityAccessor;
import headfix.SetableNameable;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.SkullBlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.text.Text;
import net.minecraft.util.Nameable;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(SkullBlockEntity.class)
public abstract class SkullBlockEntityMixin extends BlockEntity implements Nameable, SkullBlockEntityAccessor {
public abstract class SkullBlockEntityMixin extends BlockEntity implements SetableNameable {
private static final String CUSTOM_NAME_TAG = "CustomName";
private Text customName;

public SkullBlockEntityMixin(BlockEntityType<?> type, BlockPos pos, BlockState state) {
Expand All @@ -28,29 +28,37 @@ public SkullBlockEntityMixin(BlockEntityType<?> type, BlockPos pos, BlockState s
private void addCustomNameToNBT(NbtCompound nbt, CallbackInfo ci) {
if(this.customName != null) {
// saves name to nbt tag
nbt.putString("CustomName", Text.Serializer.toJson(this.customName));
nbt.putString(CUSTOM_NAME_TAG, Text.Serializer.toJson(this.customName));
}
}

// for loading CustomName from nbt to SkullBlockEntity object
@Inject(method = "readNbt", at = @At("TAIL"))
private void getCustomNameFromNBT(NbtCompound tag, CallbackInfo ci) {
if(tag.contains("CustomName", 8)) {
if(tag.contains(CUSTOM_NAME_TAG, NbtElement.STRING_TYPE)) {
// loads name from nbt tag
this.customName = Text.Serializer.fromJson(tag.getString("CustomName"));
this.customName = Text.Serializer.fromJson(tag.getString(CUSTOM_NAME_TAG));
}
}

@Override
public void setCustomName(Text customName) {
this.customName = customName;
}

// both required for Nameable
@Override
public Text getCustomName() {
return this.customName;
}

public Text getDisplayName() {
return this.customName;
@Override
public Text getName() {
// return custom name if exists
if(this.customName != null) {
return this.customName;
}

// return block name
return this.getCachedState().getBlock().getName();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
],
"depends": {
"java": ">=17",
"minecraft": "1.18.x",
"fabricloader": ">=0.12.6",
"minecraft": "1.19.x",
"fabricloader": ">=0.14.6",
"fabric": "*"
}
}
1 change: 1 addition & 0 deletions src/main/resources/headfix.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"compatibilityLevel": "JAVA_17",
"mixins": [
"AbstractSkullBlockMixin",
"BlockMixin",
"DispenserBehaviorMixin",
"SkullBlockEntityMixin"
],
Expand Down

0 comments on commit 844df95

Please sign in to comment.