Java源码示例:net.minecraft.entity.player.PlayerInventory
示例1
public MachineHandledScreen(C screenHandler, PlayerInventory playerInventory, World world, BlockPos pos, Text textComponent) {
super(screenHandler, playerInventory, textComponent);
assert isAllowed();
this.pos = pos;
this.world = world;
if (this.handler.blockEntity != null) {
ConfigurableElectricMachineBlockEntity entity = this.handler.blockEntity;
ConfigurableElectricMachineBlockEntity.SecurityInfo security = entity.getSecurity();
if (!security.hasOwner()) {
security.setOwner(this.playerInventory.player);
security.setPublicity(ConfigurableElectricMachineBlockEntity.SecurityInfo.Publicity.PRIVATE);
sendSecurityUpdate(entity);
} else if (security.getOwner().equals(playerInventory.player.getUuid())
&& !security.getUsername().equals(playerInventory.player.getEntityName())) {
security.setUsername(playerInventory.player.getEntityName());
sendSecurityUpdate(entity);
}
for (BlockFace face : BlockFace.values()) {
sideOptions.put(face, ((ConfigurableElectricMachineBlock) world.getBlockState(pos).getBlock()).getOption(world.getBlockState(pos), face));
}
}
}
示例2
private void selectFallbackSlot()
{
int fallbackSlot = getFallbackSlot();
PlayerInventory inventory = MC.player.inventory;
if(fallbackSlot == -1)
{
if(inventory.selectedSlot == 8)
inventory.selectedSlot = 0;
else
inventory.selectedSlot++;
return;
}
inventory.selectedSlot = fallbackSlot;
}
示例3
private int getFallbackSlot()
{
PlayerInventory inventory = MC.player.inventory;
for(int slot = 0; slot < 9; slot++)
{
if(slot == inventory.selectedSlot)
continue;
ItemStack stack = inventory.getStack(slot);
if(!isDamageable(stack))
return slot;
}
return -1;
}
示例4
private void updateBestRod()
{
PlayerInventory inventory = MC.player.inventory;
int selectedSlot = inventory.selectedSlot;
ItemStack selectedStack = inventory.getStack(selectedSlot);
// start with selected rod
bestRodValue = getRodValue(selectedStack);
bestRodSlot = bestRodValue > -1 ? selectedSlot : -1;
// search inventory for better rod
for(int slot = 0; slot < 36; slot++)
{
ItemStack stack = inventory.getStack(slot);
int rodValue = getRodValue(stack);
if(rodValue > bestRodValue)
{
bestRodValue = rodValue;
bestRodSlot = slot;
}
}
}
示例5
public TestDescription(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, ScreenHandlerContext context) {
super(type, syncId, playerInventory, getBlockInventory(context), null);
WGridPanel root = (WGridPanel)this.getRootPanel();
root.add(WItemSlot.of(blockInventory, 0, 4, 1), 0, 1);
root.add(new WButton(new LiteralText("Button A")), 0, 3, 4, 1);
root.add(new WButton(new LiteralText("Button B")), 5, 3, 4, 1);
root.add(new WButton(new LiteralText("Button C")), 0, 5, 4, 1);
root.add(new WButton(new LiteralText("Button D")), 5, 5, 4, 1);
root.add(new WTextField(new LiteralText("Type something...")), 0, 7, 5, 1);
root.add(createPlayerInventoryPanel(), 0, 9);
System.out.println(root.toString());
this.getRootPanel().validate(this);
}
示例6
public SawmillContainer(int windowId, PlayerInventory playerInventory, IItemHandlerModifiable inventory, IIntArray dryTimes)
{
super(TYPE, windowId);
fields = dryTimes;
wrappedInventory = new ChoppingContext(inventory, null, 0, 0, RANDOM);
world = playerInventory.player.world;
addSlot(new SlotItemHandler(inventory, 0, 56, 17));
addSlot(new SawmillFuelSlot(inventory, 1, 56, 53));
addSlot(new SawmillOutputSlot(inventory, 2, 116, 35));
bindPlayerInventory(playerInventory);
trackIntArray(fields);
}
示例7
protected void bindPlayerInventory(PlayerInventory playerInventory, int yOffset)
{
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 9; x++)
{
addSlot(new Slot(playerInventory,
x + y * 9 + 9,
8 + x * 18, yOffset + y * 18));
}
}
for (int x = 0; x < 9; x++)
{
addSlot(new Slot(playerInventory, x, 8 + x * 18, yOffset+ 58));
}
}
示例8
private int getBestSlot(BlockPos pos, boolean useSwords, boolean repairMode)
{
ClientPlayerEntity player = MC.player;
PlayerInventory inventory = player.inventory;
ItemStack heldItem = MC.player.getMainHandStack();
BlockState state = BlockUtils.getState(pos);
float bestSpeed = getMiningSpeed(heldItem, state);
int bestSlot = -1;
for(int slot = 0; slot < 9; slot++)
{
if(slot == inventory.selectedSlot)
continue;
ItemStack stack = inventory.getStack(slot);
float speed = getMiningSpeed(stack, state);
if(speed <= bestSpeed)
continue;
if(!useSwords && stack.getItem() instanceof SwordItem)
continue;
if(repairMode && isTooDamaged(stack))
continue;
bestSpeed = speed;
bestSlot = slot;
}
return bestSlot;
}
示例9
private void selectBestRod()
{
PlayerInventory inventory = MC.player.inventory;
if(bestRodSlot < 9)
{
inventory.selectedSlot = bestRodSlot;
return;
}
int firstEmptySlot = inventory.getEmptySlot();
if(firstEmptySlot != -1)
{
if(firstEmptySlot >= 9)
IMC.getInteractionManager()
.windowClick_QUICK_MOVE(36 + inventory.selectedSlot);
IMC.getInteractionManager().windowClick_QUICK_MOVE(bestRodSlot);
}else
{
IMC.getInteractionManager().windowClick_PICKUP(bestRodSlot);
IMC.getInteractionManager()
.windowClick_PICKUP(36 + inventory.selectedSlot);
scheduledWindowClick = -bestRodSlot;
}
}
示例10
@Override
public void onUpdate()
{
IClientPlayerInteractionManager im = IMC.getInteractionManager();
PlayerInventory inventory = MC.player.inventory;
if(nextTickSlot != -1)
{
im.windowClick_PICKUP(nextTickSlot);
nextTickSlot = -1;
}
ItemStack offhandStack = inventory.getStack(40);
if(offhandStack.getItem() == Items.TOTEM_OF_UNDYING)
return;
if(MC.currentScreen instanceof HandledScreen
&& !(MC.currentScreen instanceof AbstractInventoryScreen))
return;
for(int slot = 0; slot <= 36; slot++)
{
if(inventory.getStack(slot).getItem() != Items.TOTEM_OF_UNDYING)
continue;
int newTotemSlot = slot < 9 ? slot + 36 : slot;
boolean offhandEmpty = offhandStack.isEmpty();
im.windowClick_PICKUP(newTotemSlot);
im.windowClick_PICKUP(45);
if(!offhandEmpty)
nextTickSlot = newTotemSlot;
break;
}
}
示例11
@Override
public void onUpdate()
{
PlayerInventory inventory = MC.player.inventory;
if(inventory.selectedSlot == 8)
inventory.selectedSlot = 0;
else
inventory.selectedSlot++;
}
示例12
private int getOccupiedSlotWithRoomForStack(PlayerInventory playerInv, ItemStack stack) {
if (ConnectionInfo.protocolVersion <= Protocols.V1_11_2) {
if (canStackAddMore(playerInv.getStack(playerInv.selectedSlot), stack)) {
return playerInv.selectedSlot;
}
for (int j = 0; j < playerInv.main.size(); j++) {
if (canStackAddMore(playerInv.main.get(j), stack)) {
return j;
}
}
return -1;
} else {
return playerInv.getOccupiedSlotWithRoomForStack(stack);
}
}
示例13
@Override
public void onInitialize() {
Registry.register(Registry.ITEM, new Identifier(MODID, "client_gui"), new GuiItem());
GUI_BLOCK = new GuiBlock();
Registry.register(Registry.BLOCK, new Identifier(MODID, "gui"), GUI_BLOCK);
GUI_BLOCK_ITEM = new BlockItem(GUI_BLOCK, new Item.Settings().group(ItemGroup.MISC));
Registry.register(Registry.ITEM, new Identifier(MODID, "gui"), GUI_BLOCK_ITEM);
GUI_BLOCKENTITY_TYPE = BlockEntityType.Builder.create(GuiBlockEntity::new, GUI_BLOCK).build(null);
Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier(MODID, "gui"), GUI_BLOCKENTITY_TYPE);
GUI_SCREEN_HANDLER_TYPE = ScreenHandlerRegistry.registerSimple(new Identifier(MODID, "gui"), (int syncId, PlayerInventory inventory) -> {
return new TestDescription(GUI_SCREEN_HANDLER_TYPE, syncId, inventory, ScreenHandlerContext.EMPTY);
});
Optional<ModContainer> containerOpt = FabricLoader.getInstance().getModContainer("jankson");
if (containerOpt.isPresent()) {
ModContainer jankson = containerOpt.get();
System.out.println("Jankson root path: "+jankson.getRootPath());
try {
Files.list(jankson.getRootPath()).forEach((path)->{
path.getFileSystem().getFileStores().forEach((store)->{
System.out.println(" Filestore: "+store.name());
});
System.out.println(" "+path.toAbsolutePath());
});
} catch (IOException e) {
e.printStackTrace();
}
Path modJson = jankson.getPath("/fabric.mod.json");
System.out.println("Jankson fabric.mod.json path: "+modJson);
System.out.println(Files.exists(modJson) ? "Exists" : "Does Not Exist");
} else {
System.out.println("Container isn't present!");
}
}
示例14
public SyncedGuiDescription(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory) {
super(type, syncId);
this.blockInventory = null;
this.playerInventory = playerInventory;
this.world = playerInventory.player.world;
this.propertyDelegate = null;//new ArrayPropertyDelegate(1);
}
示例15
public SyncedGuiDescription(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, Inventory blockInventory, PropertyDelegate propertyDelegate) {
super(type, syncId);
this.blockInventory = blockInventory;
this.playerInventory = playerInventory;
this.world = playerInventory.player.world;
this.propertyDelegate = propertyDelegate;
if (propertyDelegate!=null && propertyDelegate.size()>0) this.addProperties(propertyDelegate);
}
示例16
/**
* Constructs a player inventory panel.
*
* @param playerInventory the player inventory
* @param label the label widget, can be null
* @since 2.0.0
*/
public WPlayerInvPanel(PlayerInventory playerInventory, @Nullable WWidget label) {
int y = 0;
this.label = label;
if (label != null) {
this.add(label, 0, 0, label.getWidth(), label.getHeight());
y += label.getHeight();
}
inv = WItemSlot.ofPlayerStorage(playerInventory);
hotbar = WItemSlot.of(playerInventory, 0, 9, 1);
this.add(inv, 0, y);
this.add(hotbar, 0, y + 58);
}
示例17
private void dropItemFromSlot(int slot, boolean dropAll)
{
PlayerInventory inv = player.inventory;
if (!inv.getInvStack(slot).isEmpty())
player.dropItem(inv.takeInvStack(slot,
dropAll ? inv.getInvStack(slot).getCount() : 1
), false, true); // scatter, keep owner
}
示例18
public void drop(int selectedSlot, boolean dropAll)
{
PlayerInventory inv = player.inventory;
if (selectedSlot == -2) // all
{
for (int i = inv.getInvSize(); i >= 0; i--)
dropItemFromSlot(i, dropAll);
}
else // one slot
{
if (selectedSlot == -1)
selectedSlot = inv.selectedSlot;
dropItemFromSlot(selectedSlot, dropAll);
}
}
示例19
public ModificationTableContainer(int windowId, PlayerInventory playerInventory, PacketBuffer extraData) {
super(ModContainers.MODIFICATIONTABLE_CONTAINER.get(), windowId);
this.tileEntity = Minecraft.getInstance().world.getTileEntity(extraData.readBlockPos());
this.playerInventory = new InvWrapper(playerInventory);
setupContainerSlots();
layoutPlayerInventorySlots(8, 84);
}
示例20
public ModificationTableContainer(int windowId, World world, BlockPos pos, PlayerInventory playerInventory) {
super(ModContainers.MODIFICATIONTABLE_CONTAINER.get(), windowId);
this.tileEntity = world.getTileEntity(pos);
this.playerInventory = new InvWrapper(playerInventory);
setupContainerSlots();
layoutPlayerInventorySlots(10, 70);
}
示例21
public GuiEnderItemStorage(ContainerEnderItemStorage container, PlayerInventory playerInv, ITextComponent title) {
super(container, playerInv, title);
passEvents = false;
if (container.chestInv.getSize() == 2) {
ySize = 222;
}
}
示例22
public ContainerEnderItemStorage(int windowId, PlayerInventory playerInv, EnderItemStorage chestInv) {
super(ModContent.containerItemStorage, windowId);
this.chestInv = chestInv;
chestInv.openInventory();
switch (chestInv.getSize()) {
case 0:
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
addSlot(new Slot(chestInv, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
addPlayerSlots(playerInv, 84);
break;
case 1:
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 9; ++col) {
addSlot(new Slot(chestInv, col + row * 9, 8 + col * 18, 18 + row * 18));
}
}
addPlayerSlots(playerInv, 85);
break;
case 2:
for (int row = 0; row < 6; ++row) {
for (int col = 0; col < 9; ++col) {
addSlot(new Slot(chestInv, col + row * 9, 8 + col * 18, 18 + row * 18));
}
}
addPlayerSlots(playerInv, 140);
break;
}
}
示例23
protected void bindPlayerInventory(PlayerInventory inventoryPlayer, int x, int y) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventoryPlayer, col + row * 9 + 9, x + col * 18, y + row * 18));
}
}
for (int slot = 0; slot < 9; slot++) {
addSlot(new Slot(inventoryPlayer, slot, x + slot * 18, y + 58));
}
}
示例24
@Override
public T create(int windowId, PlayerInventory inventory, MCDataInput packet) {
if (factory instanceof ICCLContainerFactory) {
return ((ICCLContainerFactory<T>) factory).create(windowId, inventory, packet);
}
return null;
}
示例25
private void bindPlayerInventory(PlayerInventory playerInventory)
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 9; ++j)
{
this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
for (int k = 0; k < 9; ++k)
{
this.addSlot(new Slot(playerInventory, k, 8 + k * 18, 142));
}
}
示例26
private DryingRackContainer(int windowId, PlayerInventory playerInventory, IItemHandler inventory, IIntArray dryTimes)
{
super(TYPE, windowId);
dryTimeRemainingArray = dryTimes;
addSlot(new SlotItemHandler(inventory, 0, 26, 34));
addSlot(new SlotItemHandler(inventory, 1, 62, 34));
addSlot(new SlotItemHandler(inventory, 2, 98, 34));
addSlot(new SlotItemHandler(inventory, 3, 134, 34));
bindPlayerInventory(playerInventory, 84);
this.trackIntArray(dryTimeRemainingArray);
}
示例27
public CompressorScreen(CompressorScreenHandler electricCompressorContainer, PlayerInventory inv, Text title) {
super(electricCompressorContainer, inv, title);
this.backgroundHeight = 192;
}
示例28
public PlayerInventoryGCScreen(PlayerInventoryGCScreenHandler handler, PlayerInventory inv, Text title) {
super(handler, inv, title);
}
示例29
public CoalGeneratorScreen(CoalGeneratorScreenHandler handler, PlayerInventory inv, Text title) {
super(handler, inv, inv.player.world, handler.blockEntity.getPos(), title);
this.backgroundHeight = 176;
}
示例30
public OxygenCollectorScreen(OxygenCollectorScreenHandler handler, PlayerInventory inv, Text title) {
super(handler, inv, inv.player.world, handler.blockEntity.getPos(), title);
this.backgroundHeight = 181;
}