Skip to content

Commit

Permalink
Breaking Changes: Rewrite CustomBlockEntityType And CustomEntityType
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Aug 5, 2023
1 parent f2990a2 commit 8ee6e4f
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mmodding.mmodding_lib.ducks;

import com.mmodding.mmodding_lib.library.entities.CustomEntityType;
import net.minecraft.entity.Entity;

public interface QuiltEntityTypeBuilderDuckInterface<T extends Entity> {

CustomEntityType<T> mmodding_lib$buildCustom();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mmodding.mmodding_lib.library.blockentities;

import com.mmodding.mmodding_lib.library.utils.Registrable;
import com.mmodding.mmodding_lib.library.utils.RegistrationUtils;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.Identifier;

public interface BlockEntityTypeRegistrable<T extends BlockEntity> extends Registrable {

default void register(Identifier identifier) {
if (this instanceof CustomBlockEntityType<T> blockEntityType && this.isNotRegistered()) {
RegistrationUtils.registerBlockEntityType(identifier, blockEntityType);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
package com.mmodding.mmodding_lib.library.blockentities;

import com.mmodding.mmodding_lib.mixin.accessors.BlockEntityTypeAccessor;
import com.google.common.collect.ImmutableSet;
import com.mmodding.mmodding_lib.mixin.accessors.BlockEntityTypeBuilderAccessor;
import com.mojang.datafixers.types.Type;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

public class CustomBlockEntityType<T extends BlockEntity> {
public class CustomBlockEntityType<T extends BlockEntity> extends BlockEntityType<T> implements BlockEntityTypeRegistrable<T> {

private final BlockEntityType.BlockEntityFactory<? extends T> factory;
private final List<Block> blockList;
private BlockEntityType<T> blockEntityType = null;
private final AtomicBoolean registered = new AtomicBoolean(false);

public CustomBlockEntityType(BlockEntityType.BlockEntityFactory<? extends T> factory, Block... blocks) {
this.factory = factory;
this.blockList = new ArrayList<>(blocks.length);
Collections.addAll(this.blockList, blocks);
public CustomBlockEntityType(BlockEntityFactory<? extends T> factory, Set<Block> blocks, Type<?> type) {
super(factory, blocks, type);
}

public CustomBlockEntityType<T> createAndRegister(Identifier identifier) {
this.blockEntityType = BlockEntityTypeAccessor.create(identifier.toString(), BlockEntityType.Builder.create(factory, blockList.toArray(new Block[0])));
return this;
public static <T extends BlockEntity> CustomBlockEntityType<T> create(BlockEntityType.BlockEntityFactory<T> factory, Type<?> dataFixerType, Block... blocks) {
return Builder.create(factory, blocks).buildCustom(dataFixerType);
}

@Nullable
public BlockEntityType<T> getBlockEntityTypeIfCreated() {
return blockEntityType;
@Override
public boolean isNotRegistered() {
return !this.registered.get();
}

@Override
public void setRegistered() {
this.registered.set(true);
}

public static class Builder<T extends BlockEntity> extends BlockEntityType.Builder<T> {

private Builder(BlockEntityType.BlockEntityFactory<? extends T> factory, Set<Block> blocks) {
super(factory, blocks);
}

public static <T extends BlockEntity> Builder<T> create(BlockEntityType.BlockEntityFactory<? extends T> factory, Block... blocks) {
return new Builder<>(factory, ImmutableSet.copyOf(blocks));
}

public CustomBlockEntityType<T> buildCustom(Type<?> type) {
BlockEntityTypeBuilderAccessor accessor = (BlockEntityTypeBuilderAccessor) this;
return new CustomBlockEntityType<>(accessor.getFactory(), accessor.getBlocks(), type);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package com.mmodding.mmodding_lib.library.entities;

import com.google.common.collect.ImmutableSet;
import com.mmodding.mmodding_lib.ducks.QuiltEntityTypeBuilderDuckInterface;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import org.jetbrains.annotations.Nullable;
import org.quiltmc.qsl.entity.api.QuiltEntityTypeBuilder;
import org.quiltmc.qsl.entity.impl.QuiltEntityType;

import java.util.concurrent.atomic.AtomicBoolean;

public class CustomEntityType<T extends Entity> extends EntityType<T> implements EntityTypeRegistrable<T> {
public class CustomEntityType<T extends Entity> extends QuiltEntityType<T> implements EntityTypeRegistrable<T> {

private final AtomicBoolean registered = new AtomicBoolean(false);

public CustomEntityType(EntityFactory<T> factory, SpawnGroup spawnGroup, boolean bl, boolean summonable, boolean fireImmune, boolean spawnableFarFromPlayer, ImmutableSet<Block> spawnBlocks, EntityDimensions entityDimensions, int maxTrackDistance, int trackTickInterval) {
super(factory, spawnGroup, bl, summonable, fireImmune, spawnableFarFromPlayer, spawnBlocks, entityDimensions, maxTrackDistance, trackTickInterval);
public CustomEntityType(EntityFactory<T> factory, SpawnGroup spawnGroup, boolean saveable, boolean summonable, boolean fireImmune, boolean spawnableFarFromPlayer, ImmutableSet<Block> spawnBlocks, EntityDimensions entityDimensions, int maxTrackDistance, int trackTickInterval, @Nullable Boolean alwaysUpdateVelocity) {
super(factory, spawnGroup, saveable, summonable, fireImmune, spawnableFarFromPlayer, spawnBlocks, entityDimensions, maxTrackDistance, trackTickInterval, alwaysUpdateVelocity);
}

@SuppressWarnings("unchecked")
public static <T extends Entity> CustomEntityType<T> create(QuiltEntityTypeBuilder<T> builder) {
return ((QuiltEntityTypeBuilderDuckInterface<T>) builder).mmodding_lib$buildCustom();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
public interface EntityTypeRegistrable<T extends Entity> extends Registrable {

default void register(Identifier identifier) {
if (this instanceof CustomEntityType<T> && this.isNotRegistered()) {
RegistrationUtils.registerEntityType(identifier, (CustomEntityType<T>) this);
if (this instanceof CustomEntityType<T> entityType && this.isNotRegistered()) {
RegistrationUtils.registerEntityType(identifier, entityType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mmodding.mmodding_lib.library.blocks.BlockWithItem;
import com.mmodding.mmodding_lib.library.portals.CustomSquaredPortalBlock;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
Expand Down Expand Up @@ -68,6 +70,10 @@ public static <T extends Entity> void registerEntityType(Identifier identifier,
Registry.register(Registry.ENTITY_TYPE, identifier, entityType);
}

public static <T extends BlockEntity> void registerBlockEntityType(Identifier identifier, BlockEntityType<T> blockEntityType) {
Registry.register(Registry.BLOCK_ENTITY_TYPE, identifier, blockEntityType);
}

public static <T extends ScreenHandler> void registerScreenHandlerType(Identifier identifier, ScreenHandlerType<T> screenHandlerType) {
Registry.register(Registry.SCREEN_HANDLER, identifier, screenHandlerType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mmodding.mmodding_lib.mixin.accessors;

import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.Set;

@Mixin(BlockEntityType.Builder.class)
public interface BlockEntityTypeBuilderAccessor {

@Accessor
<T extends BlockEntity> BlockEntityType.BlockEntityFactory<T> getFactory();

@Accessor
Set<Block> getBlocks();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.mmodding.mmodding_lib.mixin.injectors;

import com.google.common.collect.ImmutableSet;
import com.mmodding.mmodding_lib.ducks.QuiltEntityTypeBuilderDuckInterface;
import com.mmodding.mmodding_lib.library.entities.CustomEntityType;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import org.jetbrains.annotations.NotNull;
import org.quiltmc.qsl.entity.api.QuiltEntityTypeBuilder;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(value = QuiltEntityTypeBuilder.class, remap = false)
public class QuiltEntityTypeBuilderMixin<T extends Entity> implements QuiltEntityTypeBuilderDuckInterface<T> {

@Shadow
private EntityType.EntityFactory<T> factory;

@Shadow
@NotNull
private SpawnGroup spawnGroup;

@Shadow
private boolean saveable;

@Shadow
private boolean summonable;

@Shadow
private boolean fireImmune;

@Shadow
private boolean spawnableFarFromPlayer;

@Shadow
private ImmutableSet<Block> canSpawnInside;

@Shadow
private EntityDimensions dimensions;

@Shadow
private int maxTrackingRange;

@Shadow
private int trackingTickInterval;

@Shadow
private Boolean alwaysUpdateVelocity;

@Override
public CustomEntityType<T> mmodding_lib$buildCustom() {
return new CustomEntityType<>(
this.factory,
this.spawnGroup,
this.saveable,
this.summonable,
this.fireImmune,
this.spawnableFarFromPlayer,
this.canSpawnInside,
this.dimensions,
this.maxTrackingRange,
this.trackingTickInterval,
this.alwaysUpdateVelocity
);
}
}
8 changes: 6 additions & 2 deletions src/main/resources/mmodding_lib.accesswidener
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
accessWidener v1 named

accessible class net/minecraft/entity/passive/ParrotEntity$ParrotWanderGoal
accessible class net/minecraft/recipe/Ingredient$Entry
accessible class net/minecraft/recipe/Ingredient$StackEntry
accessible class net/minecraft/recipe/Ingredient$TagEntry
accessible class net/minecraft/entity/passive/ParrotEntity$ParrotWanderGoal
accessible class net/minecraft/world/gen/DensityFunctions$StructureWeightSamplerMarker

extendable class net/minecraft/block/entity/BlockEntityType$Builder
extendable class net/minecraft/world/gen/chunk/NoiseChunkGenerator

mutable field net/minecraft/world/gen/chunk/ChunkNoiseSampler blockStateSampler Lnet/minecraft/world/gen/chunk/ChunkNoiseSampler$BlockStateSampler;
mutable field net/minecraft/world/dimension/AreaHelper width I
mutable field net/minecraft/world/gen/chunk/ChunkNoiseSampler blockStateSampler Lnet/minecraft/world/gen/chunk/ChunkNoiseSampler$BlockStateSampler;

accessible method net/minecraft/block/entity/BlockEntityType$Builder <init> (Lnet/minecraft/block/entity/BlockEntityType$BlockEntityFactory;Ljava/util/Set;)V
accessible method net/minecraft/recipe/Ingredient$StackEntry <init> (Lnet/minecraft/item/ItemStack;)V
accessible method net/minecraft/recipe/Ingredient$TagEntry <init> (Lnet/minecraft/tag/TagKey;)V
accessible method net/minecraft/world/gen/OreVeinCreator create (Lnet/minecraft/world/gen/DensityFunction;Lnet/minecraft/world/gen/DensityFunction;Lnet/minecraft/world/gen/DensityFunction;Lnet/minecraft/util/random/PositionalRandomFactory;)Lnet/minecraft/world/gen/chunk/ChunkNoiseSampler$BlockStateSampler;

extendable method net/minecraft/world/dimension/AreaHelper getLowerCorner (Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/util/math/BlockPos;
extendable method net/minecraft/world/dimension/AreaHelper getWidth (Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/math/Direction;)I
extendable method net/minecraft/world/dimension/AreaHelper method_30491 (Lnet/minecraft/util/math/BlockPos$Mutable;I)Z
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/mmodding_lib.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mixins": [
"accessors.AreaHelperAccessor",
"accessors.BlockEntityTypeAccessor",
"accessors.BlockEntityTypeBuilderAccessor",
"accessors.BlocksAccessor",
"accessors.BrewingRecipeRegistryAccessor",
"accessors.BuiltinNoiseParametersAccessor",
Expand Down Expand Up @@ -43,6 +44,7 @@
"injectors.PlayerEntityMixin",
"injectors.PointedDripstoneBlockMixin",
"injectors.PortalForcerMixin",
"injectors.QuiltEntityTypeBuilderMixin",
"injectors.ServerPlayerEntityMixin",
"injectors.ServerWorldMixin",
"injectors.ShearsItemMixin",
Expand Down

0 comments on commit 8ee6e4f

Please sign in to comment.