-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking Changes: Rewrite CustomBlockEntityType And CustomEntityType
- Loading branch information
1 parent
f2990a2
commit 8ee6e4f
Showing
10 changed files
with
175 additions
and
28 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/main/java/com/mmodding/mmodding_lib/ducks/QuiltEntityTypeBuilderDuckInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
15 changes: 15 additions & 0 deletions
15
...main/java/com/mmodding/mmodding_lib/library/blockentities/BlockEntityTypeRegistrable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
55 changes: 35 additions & 20 deletions
55
src/main/java/com/mmodding/mmodding_lib/library/blockentities/CustomBlockEntityType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
16 changes: 12 additions & 4 deletions
16
src/main/java/com/mmodding/mmodding_lib/library/entities/CustomEntityType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/mmodding/mmodding_lib/mixin/accessors/BlockEntityTypeBuilderAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/mmodding/mmodding_lib/mixin/injectors/QuiltEntityTypeBuilderMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters