Can now define an inventory to open after closing another + added a bunch of inventory type

This commit is contained in:
Felix Cravic 2020-04-29 17:27:58 +02:00
parent 0782ee7fe5
commit c2af4be2fa
10 changed files with 137 additions and 3 deletions

View File

@ -127,6 +127,7 @@ public class PlayerInit {
return;
short blockId = player.getInstance().getBlockId(event.getBlockPosition());
player.sendMessage("id: " + blockId);
player.sendMessage("block alternative id: " + Block.getBlockAlternative(blockId).getId());
});
@ -154,10 +155,10 @@ public class PlayerInit {
});
player.addEventCallback(PlayerSpawnEvent.class, event -> {
player.setGameMode(GameMode.CREATIVE);
player.setGameMode(GameMode.SURVIVAL);
player.teleport(new Position(0, 75, 0));
ItemStack item = new ItemStack((short) 1, (byte) 43);
ItemStack item = new ItemStack(Material.STONE, (byte) 43);
item.setDisplayName("Item name");
item.getLore().add("a lore line");
player.getInventory().addItemStack(item);

View File

@ -16,7 +16,6 @@ public class StoneBlock extends CustomBlock {
@Override
public void onPlace(Instance instance, BlockPosition blockPosition, Data data) {
System.out.println("PLACED");
instance.refreshBlockId(blockPosition, Block.LAVA);
}
@Override

View File

@ -5,6 +5,7 @@ import net.minestom.server.inventory.Inventory;
public class InventoryCloseEvent extends Event {
private Inventory inventory;
private Inventory newInventory;
public InventoryCloseEvent(Inventory inventory) {
this.inventory = inventory;
@ -16,4 +17,17 @@ public class InventoryCloseEvent extends Event {
public Inventory getInventory() {
return inventory;
}
public Inventory getNewInventory() {
return newInventory;
}
/**
* Can be used to open a new inventory after closing the previous one
*
* @param newInventory the inventory to open, can be null
*/
public void setNewInventory(Inventory newInventory) {
this.newInventory = newInventory;
}
}

View File

@ -9,6 +9,7 @@ import net.minestom.server.inventory.condition.InventoryCondition;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.SetSlotPacket;
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
import net.minestom.server.network.packet.server.play.WindowPropertyPacket;
import java.util.Arrays;
import java.util.Collections;
@ -149,6 +150,14 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
return windowItemsPacket;
}
protected void sendProperty(InventoryProperty property, short value) {
WindowPropertyPacket windowPropertyPacket = new WindowPropertyPacket();
windowPropertyPacket.windowId = getWindowId();
windowPropertyPacket.property = property.getProperty();
windowPropertyPacket.value = value;
sendPacketToViewers(windowPropertyPacket);
}
private void setCursorPlayerItem(Player player, ItemStack itemStack) {
this.cursorPlayersItem.put(player, itemStack);
}

View File

@ -0,0 +1,16 @@
package net.minestom.server.inventory.type;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryProperty;
import net.minestom.server.inventory.InventoryType;
public class AnvilInventory extends Inventory {
public AnvilInventory(String title) {
super(InventoryType.ANVIL, title);
}
public void setRepairCost(short cost) {
sendProperty(InventoryProperty.ANVIL_REPAIR_COST, cost);
}
}

View File

@ -0,0 +1,25 @@
package net.minestom.server.inventory.type;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryProperty;
import net.minestom.server.inventory.InventoryType;
public class BeaconInventory extends Inventory {
public BeaconInventory(String title) {
super(InventoryType.BEACON, title);
}
public void setPowerLevel(short powerLevel) {
sendProperty(InventoryProperty.BEACON_POWER_LEVEL, powerLevel);
}
public void setFirstPotionEffect(short firstPotionEffect) {
sendProperty(InventoryProperty.BEACON_FIRST_POTION, firstPotionEffect);
}
public void setSecondPotionEffect(short secondPotionEffect) {
sendProperty(InventoryProperty.BEACON_SECOND_POTION, secondPotionEffect);
}
}

View File

@ -0,0 +1,21 @@
package net.minestom.server.inventory.type;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryProperty;
import net.minestom.server.inventory.InventoryType;
public class BrewingStandInventory extends Inventory {
public BrewingStandInventory(String title) {
super(InventoryType.BREWING_STAND, title);
}
public void setBrewTime(short brewTime) {
sendProperty(InventoryProperty.BREWING_STAND_BREW_TIME, brewTime);
}
public void setFuelTime(short fuelTime) {
sendProperty(InventoryProperty.BREWING_STAND_FUEL_TIME, fuelTime);
}
}

View File

@ -0,0 +1,11 @@
package net.minestom.server.inventory.type;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
public class EnchantmentTableInventory extends Inventory {
public EnchantmentTableInventory(String title) {
super(InventoryType.ENCHANTMENT, title);
}
}

View File

@ -0,0 +1,33 @@
package net.minestom.server.inventory.type;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryProperty;
import net.minestom.server.inventory.InventoryType;
public class FurnaceInventory extends Inventory {
public FurnaceInventory(String title) {
super(InventoryType.FURNACE, title);
}
/**
* Represent the amount of tick until the fire icon come empty`
*
* @param remainingFuelTick
*/
public void setRemainingFuelTick(short remainingFuelTick) {
sendProperty(InventoryProperty.FURNACE_FIRE_ICON, remainingFuelTick);
}
public void setMaximumFuelBurnTime(short maximumFuelBurnTime) {
sendProperty(InventoryProperty.FURNACE_MAXIMUM_FUEL_BURN_TIME, maximumFuelBurnTime);
}
public void setProgressArrow(short progressArrow) {
sendProperty(InventoryProperty.FURNACE_PROGRESS_ARROW, progressArrow);
}
public void setMaximumProgress(short maximumProgress) {
sendProperty(InventoryProperty.FURNACE_MAXIMUM_PROGRESS, maximumProgress);
}
}

View File

@ -91,7 +91,12 @@ public class WindowListener {
// if windowId == 0 then it is player's inventory, meaning that they hadn't been any open inventory packet
InventoryCloseEvent inventoryCloseEvent = new InventoryCloseEvent(player.getOpenInventory());
player.callEvent(InventoryCloseEvent.class, inventoryCloseEvent);
player.closeInventory();
Inventory newInventory = inventoryCloseEvent.getNewInventory();
if (newInventory != null)
player.openInventory(newInventory);
}
}