Skip to content

Commit

Permalink
Metal Detection Door
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiYueCommentary committed Sep 21, 2024
1 parent c23b25c commit 71d6369
Show file tree
Hide file tree
Showing 19 changed files with 938 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ziyue.tjmetro.fabric.mixin;

import net.minecraft.entity.EntityType;
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
import net.minecraft.entity.vehicle.StorageMinecartEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import ziyue.tjmetro.mapping.ContainerAccessor;

/**
* @author ZiYueCommentary
* @see ziyue.tjmetro.mapping.ContainerAccessor
* @since 1.0.0-beta-2
*/

@Mixin(StorageMinecartEntity.class)
public abstract class StorageMinecartEntityMixin extends AbstractMinecartEntity implements Inventory, NamedScreenHandlerFactory, ContainerAccessor
{
@Shadow private DefaultedList<ItemStack> inventory;

protected StorageMinecartEntityMixin(EntityType<?> entityType, World world) {
super(entityType, world);
}

@Override
public DefaultedList<ItemStack> tianjin_Metro$getInventory() {
return this.inventory;
}
}
16 changes: 16 additions & 0 deletions fabric/src/main/java/ziyue/tjmetro/mapping/ContainerAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ziyue.tjmetro.mapping;

import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;
import ziyue.tjmetro.fabric.mixin.StorageMinecartEntityMixin;

/**
* @author ZiYueCommentary
* @see StorageMinecartEntityMixin
* @since 1.0.0-beta-2
*/

public interface ContainerAccessor
{
DefaultedList<ItemStack> tianjin_Metro$getInventory();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ziyue.tjmetro.mapping;

import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;
import org.mtr.mapping.annotation.MappedMethod;
import org.mtr.mapping.tool.HolderBase;

/**
* @author ZiYueCommentary
* @since 1.0.0-beta-2
*/

public class DefaultedItemStackList extends HolderBase<DefaultedList<ItemStack>>
{
public DefaultedItemStackList(DefaultedItemStackList data) {
super(data.data);
}

public DefaultedItemStackList(DefaultedList<ItemStack> data) {
super(data);
}

public static DefaultedItemStackList ofSize(int size) {
return new DefaultedItemStackList(DefaultedList.ofSize(size, ItemStack.EMPTY));
}

@MappedMethod
public int size() {
return this.data.size();
}

@MappedMethod
public void set(int i, ItemStack stack) {
this.data.set(i, stack);
}

@MappedMethod
public ItemStack get(int i) {
return this.data.get(i);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package ziyue.tjmetro.mapping;

/**
* An entity for GUI of Metal Detection Door. This entity is a minecart-with-chest.
*
* @author ZiYueCommentary
* @see BlockMetalDetectionDoor
* @since 1.0.0-beta-2
*/

#if MC_VERSION >= "11902"

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.vehicle.ChestMinecartEntity;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.text.Text;
import org.mtr.mapping.holder.BlockPos;
import org.mtr.mapping.holder.World;
import ziyue.tjmetro.mod.block.BlockMetalDetectionDoor;

public class MetalDetectionDoorEntity extends ChestMinecartEntity
{
public final BlockMetalDetectionDoor.BlockEntity blockEntity;

public MetalDetectionDoorEntity(World world, BlockPos blockPos, BlockMetalDetectionDoor.BlockEntity blockEntity) {
super(world.data, blockPos.getX(), -1, blockPos.getZ());
this.blockEntity = blockEntity;
for (int i = 0; i < blockEntity.inventory.size(); i++) {
this.getInventory().set(i, blockEntity.inventory.get(i));
}
}

@Override
public ScreenHandler getScreenHandler(int syncId, PlayerInventory playerInventory) {
return new GenericContainerScreenHandler(ScreenHandlerType.GENERIC_9X1, syncId, playerInventory, this, 1)
{
@Override
public void onClosed(PlayerEntity player) {
super.onClosed(player);
final MetalDetectionDoorEntity entity = (MetalDetectionDoorEntity) this.getInventory();
entity.blockEntity.setData(new DefaultedItemStackList(entity.getInventory()));
entity.getInventory().clear();
entity.kill();
}
};
}

@Override
public boolean canPlayerUse(PlayerEntity player) {
return true;
}

@Override
public boolean hasNoGravity() {
return true;
}

@Override
public int getMaxCountPerStack() {
return 1;
}

@Override
public Text getDisplayName() {
return Text.translatable("gui.tjmetro.metal_detection_door");
}
}

#else

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.vehicle.ChestMinecartEntity;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import org.mtr.mapping.holder.BlockPos;
import org.mtr.mapping.holder.World;
import ziyue.tjmetro.mod.block.BlockMetalDetectionDoor;

public class MetalDetectionDoorEntity extends ChestMinecartEntity
{
public final BlockMetalDetectionDoor.BlockEntity blockEntity;

public MetalDetectionDoorEntity(World world, BlockPos blockPos, BlockMetalDetectionDoor.BlockEntity blockEntity) {
super(world.data, blockPos.getX(), -1, blockPos.getZ());
this.blockEntity = blockEntity;
for (int i = 0; i < blockEntity.inventory.size(); i++) {
((ContainerAccessor) this).tianjin_Metro$getInventory().set(i, blockEntity.inventory.get(i));
}
}

@Override
public ScreenHandler getScreenHandler(int syncId, PlayerInventory playerInventory) {
return new GenericContainerScreenHandler(ScreenHandlerType.GENERIC_9X1, syncId, playerInventory, this, 1)
{
@Override
public void close(PlayerEntity player) {
super.close(player);
final MetalDetectionDoorEntity entity = (MetalDetectionDoorEntity) this.getInventory();
entity.blockEntity.setData(new DefaultedItemStackList(((ContainerAccessor) entity).tianjin_Metro$getInventory()));
((ContainerAccessor) entity).tianjin_Metro$getInventory().clear();
entity.kill();
}
};
}

@Override
public boolean canPlayerUse(PlayerEntity player) {
return true;
}

@Override
public boolean hasNoGravity() {
return true;
}

@Override
public int getMaxCountPerStack() {
return 1;
}

@Override
public Text getDisplayName() {
return new TranslatableText("gui.tjmetro.metal_detection_door");
}
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ziyue.tjmetro.mapping;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;
import org.mtr.mapping.holder.PlayerEntity;

import java.util.function.Function;

/**
* @author ZiYueCommentary
* @since 1.0.0-beta-2
*/

public interface PlayerInventoryHelper
{
static void clearItems(PlayerEntity player, Function<Item, Boolean> filter) {
#if MC_VERSION <= "11605"
final DefaultedList<ItemStack> main = player.data.inventory.main;
for (int i = 0; i < main.size(); i++) {
if (filter.apply(main.get(i).getItem())) main.set(i, ItemStack.EMPTY);
}

final DefaultedList<ItemStack> armor = player.data.inventory.armor;
for (int i = 0; i < armor.size(); i++) {
if (filter.apply(armor.get(i).getItem())) armor.set(i, ItemStack.EMPTY);
}

final DefaultedList<ItemStack> offHand = player.data.inventory.offHand;
for (int i = 0; i < offHand.size(); i++) {
if (filter.apply(offHand.get(i).getItem())) offHand.set(i, ItemStack.EMPTY);
}
#else
final DefaultedList<ItemStack> main = player.data.getInventory().main;
for (int i = 0; i < main.size(); i++) {
if (filter.apply(main.get(i).getItem())) main.set(i, ItemStack.EMPTY);
}

final DefaultedList<ItemStack> armor = player.data.getInventory().armor;
for (int i = 0; i < armor.size(); i++) {
if (filter.apply(armor.get(i).getItem())) armor.set(i, ItemStack.EMPTY);
}

final DefaultedList<ItemStack> offHand = player.data.getInventory().offHand;
for (int i = 0; i < offHand.size(); i++) {
if (filter.apply(offHand.get(i).getItem())) offHand.set(i, ItemStack.EMPTY);
}
#endif
}
}
25 changes: 22 additions & 3 deletions fabric/src/main/java/ziyue/tjmetro/mapping/RegistryHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ziyue.tjmetro.mapping;

import net.minecraft.item.ItemStack;
import org.mtr.mapping.holder.Identifier;
import org.mtr.mapping.holder.Item;
import org.mtr.mapping.registry.BlockRegistryObject;
Expand All @@ -9,20 +10,38 @@
import java.lang.reflect.InvocationTargetException;

/**
* Converting block to item. This may not be stable since it uses reflection.
*
* @author ZiYueCommentary
* @since 1.0.0-beta-2
*/

// This is a very mad way but I have no choice.
public interface RegistryHelper
{
static ItemRegistryObject RegistryObjectBlock2Item(BlockRegistryObject fabric, Identifier forge)
throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
// This is a very mad way but I have no choice.
Class<ItemRegistryObject> clazz = ItemRegistryObject.class;
Constructor<ItemRegistryObject> constructor = clazz.getDeclaredConstructor(Item.class);
constructor.setAccessible(true);
return constructor.newInstance(fabric.get().asItem());
}

static ItemStack cloneSingleItemStack(ItemStack itemStack) {
return new ItemStack(itemStack.getItem());
}

static Identifier getIdentifierByItem(net.minecraft.item.Item item) {
#if MC_VERSION >= "11902"
return new Identifier(net.minecraft.registry.Registries.ITEM.getId(item));
#else
return new Identifier(net.minecraft.util.registry.Registry.ITEM.getId(item));
#endif
}

static ItemStack getItemStackByIdentifier(Identifier identifier) {
#if MC_VERSION >= "11902"
return net.minecraft.registry.Registries.ITEM.get(identifier.data).getDefaultStack();
#else
return net.minecraft.util.registry.Registry.ITEM.get(identifier.data).getDefaultStack();
#endif
}
}
Loading

0 comments on commit 71d6369

Please sign in to comment.