-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c23b25c
commit 71d6369
Showing
19 changed files
with
938 additions
and
21 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
fabric/src/main/java/ziyue/tjmetro/fabric/mixin/StorageMinecartEntityMixin.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,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
16
fabric/src/main/java/ziyue/tjmetro/mapping/ContainerAccessor.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,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(); | ||
} |
41 changes: 41 additions & 0 deletions
41
fabric/src/main/java/ziyue/tjmetro/mapping/DefaultedItemStackList.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,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); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
fabric/src/main/java/ziyue/tjmetro/mapping/MetalDetectionDoorEntity.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,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 |
50 changes: 50 additions & 0 deletions
50
fabric/src/main/java/ziyue/tjmetro/mapping/PlayerInventoryHelper.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,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 | ||
} | ||
} |
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
Oops, something went wrong.