-
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.
Add BlockEntityTypeBlockSupportCallback
- Loading branch information
1 parent
28c1deb
commit 909c7b2
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/com/mmodding/mmodding_lib/ducks/BlockEntityTypeDuckInterface.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,18 @@ | ||
package com.mmodding.mmodding_lib.ducks; | ||
|
||
import com.mmodding.mmodding_lib.library.blockentities.BlockEntityTypeBlockSupportCallback; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.block.entity.BlockEntityType; | ||
import org.quiltmc.qsl.base.api.event.Event; | ||
|
||
public interface BlockEntityTypeDuckInterface { | ||
|
||
Event<BlockEntityTypeBlockSupportCallback> mmodding_lib$supportBlocksCallback(); | ||
|
||
static <T extends BlockEntity> BlockEntityTypeDuckInterface get(BlockEntityType<T> blockEntityType) { | ||
if (!(blockEntityType instanceof BlockEntityTypeDuckInterface)) { | ||
throw new IllegalArgumentException("Unsupported BlockEntityType: " + blockEntityType); | ||
} | ||
return (BlockEntityTypeDuckInterface) blockEntityType; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../com/mmodding/mmodding_lib/library/blockentities/BlockEntityTypeBlockSupportCallback.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,18 @@ | ||
package com.mmodding.mmodding_lib.library.blockentities; | ||
|
||
import com.mmodding.mmodding_lib.ducks.BlockEntityTypeDuckInterface; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.block.entity.BlockEntityType; | ||
import org.quiltmc.qsl.base.api.event.Event; | ||
|
||
import java.util.List; | ||
|
||
public interface BlockEntityTypeBlockSupportCallback { | ||
|
||
void onSupportedBlocks(List<Block> supportedBlocks); | ||
|
||
static <T extends BlockEntity> Event<BlockEntityTypeBlockSupportCallback> blockEntityType(BlockEntityType<T> blockEntityType) { | ||
return BlockEntityTypeDuckInterface.get(blockEntityType).mmodding_lib$supportBlocksCallback(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/mmodding/mmodding_lib/mixin/injectors/BlockEntityTypeMixin.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,48 @@ | ||
package com.mmodding.mmodding_lib.mixin.injectors; | ||
|
||
import com.mmodding.mmodding_lib.ducks.BlockEntityTypeDuckInterface; | ||
import com.mmodding.mmodding_lib.library.blockentities.BlockEntityTypeBlockSupportCallback; | ||
import com.mojang.datafixers.types.Type; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.block.entity.BlockEntityType; | ||
import org.quiltmc.qsl.base.api.event.Event; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Mixin(BlockEntityType.class) | ||
public class BlockEntityTypeMixin<T extends BlockEntity> implements BlockEntityTypeDuckInterface { | ||
|
||
@Unique | ||
private final Event<BlockEntityTypeBlockSupportCallback> supportBlocksCallBack = Event.create(BlockEntityTypeBlockSupportCallback.class, callbacks -> supportedBlocks -> { | ||
for (BlockEntityTypeBlockSupportCallback callback : callbacks) { | ||
callback.onSupportedBlocks(supportedBlocks); | ||
} | ||
}); | ||
|
||
@Shadow | ||
private Set<Block> blocks; | ||
|
||
@Inject(method = "<init>", at = @At("TAIL")) | ||
private void init(BlockEntityType.BlockEntityFactory<T> factory, Set<Block> blocks, Type<?> type, CallbackInfo ci) { | ||
Set<Block> previousBlocks = new HashSet<>(this.blocks); | ||
List<Block> eventBlocks = new ArrayList<>(previousBlocks.stream().toList()); | ||
this.mmodding_lib$supportBlocksCallback().invoker().onSupportedBlocks(eventBlocks); | ||
previousBlocks.addAll(eventBlocks); | ||
this.blocks = previousBlocks; | ||
} | ||
|
||
@Override | ||
public Event<BlockEntityTypeBlockSupportCallback> mmodding_lib$supportBlocksCallback() { | ||
return this.supportBlocksCallBack; | ||
} | ||
} |
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