Skip to content

Commit

Permalink
Inventories!
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP committed Dec 9, 2022
1 parent 949d2f3 commit 35c6b74
Show file tree
Hide file tree
Showing 29 changed files with 1,681 additions and 79 deletions.
11 changes: 10 additions & 1 deletion src/main/java/com/loohp/limbo/Limbo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.loohp.limbo.consolegui.GUI;
import com.loohp.limbo.events.EventsManager;
import com.loohp.limbo.file.ServerProperties;
import com.loohp.limbo.inventory.AnvilInventory;
import com.loohp.limbo.inventory.CustomInventory;
import com.loohp.limbo.inventory.Inventory;
import com.loohp.limbo.inventory.InventoryHolder;
Expand Down Expand Up @@ -626,7 +627,15 @@ public Inventory createInventory(InventoryType type, InventoryHolder holder) {
}

public Inventory createInventory(Component title, InventoryType type, InventoryHolder holder) {
throw new UnsupportedOperationException("This function has not been implemented yet.");
if (!type.isCreatable()) {
throw new UnsupportedOperationException("This InventoryType cannot be created.");
}
switch (type) {
case ANVIL:
return new AnvilInventory(title, holder);
default:
throw new UnsupportedOperationException("This InventoryType has not been implemented yet.");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of Limbo.
*
* Copyright (C) 2022. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2022. Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.loohp.limbo.events.inventory;

import com.loohp.limbo.events.Cancellable;
import com.loohp.limbo.inventory.InventoryView;

public class AnvilRenameInputEvent extends InventoryEvent implements Cancellable {

private boolean cancelled;
private String input;

public AnvilRenameInputEvent(InventoryView inventoryView, String input) {
super(inventoryView, inventoryView.getTopInventory());
this.input = input;
this.cancelled = false;
}

public void setInput(String input) {
this.input = input;
}

public String getInput() {
return input;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}

@Override
public boolean isCancelled() {
return cancelled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,86 @@
package com.loohp.limbo.events.inventory;

import com.loohp.limbo.events.Cancellable;
import com.loohp.limbo.inventory.Inventory;
import com.loohp.limbo.inventory.ClickType;
import com.loohp.limbo.inventory.InventoryAction;
import com.loohp.limbo.inventory.InventoryType;
import com.loohp.limbo.inventory.InventoryView;
import com.loohp.limbo.player.Player;
import com.loohp.limbo.inventory.ItemStack;

public class InventoryClickEvent extends InventoryEvent implements Cancellable {

private boolean cancelled;
private final ClickType click;
private final InventoryAction action;
private InventoryType.SlotType type;
private int whichSlot;
private int rawSlot;
private ItemStack current;
private int hotbarKey;

public InventoryClickEvent(Player player, InventoryView inventoryView, Inventory clickedInventory) {
super(player, inventoryView, clickedInventory);
public InventoryClickEvent(InventoryView view, InventoryType.SlotType type, int rawSlot, ClickType click, InventoryAction action) {
super(view, view.getInventory(rawSlot));
this.type = type;
this.rawSlot = rawSlot;
this.whichSlot = view.convertSlot(rawSlot);
this.click = click;
this.action = action;
this.current = null;
this.hotbarKey = -1;
this.cancelled = false;
}

public InventoryClickEvent(InventoryView view, InventoryType.SlotType type, int rawSlot, ClickType click, InventoryAction action, int hotbarKey) {
this(view, type, rawSlot, click, action);
this.hotbarKey = hotbarKey;
}

public ClickType getClick() {
return click;
}

public InventoryAction getAction() {
return action;
}

public InventoryType.SlotType getType() {
return type;
}

public int getWhichSlot() {
return whichSlot;
}

public int getRawSlot() {
return rawSlot;
}

public int getHotbarKey() {
return hotbarKey;
}

public ItemStack getCarriedItem() {
return getView().getCarriedItem();
}

@Deprecated
public void setCarriedItem(ItemStack stack) {
getView().setCarriedItem(stack);
}

public ItemStack getCurrentItem() {
if (type == InventoryType.SlotType.OUTSIDE) {
return current;
}
return getView().getItem(rawSlot);
}

public void setCurrentItem(ItemStack stack) {
if (type == InventoryType.SlotType.OUTSIDE) {
current = stack;
} else {
getView().setItem(rawSlot, stack);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
package com.loohp.limbo.events.inventory;

import com.loohp.limbo.inventory.InventoryView;
import com.loohp.limbo.player.Player;

public class InventoryCloseEvent extends InventoryEvent {

public InventoryCloseEvent(Player player, InventoryView inventoryView) {
super(player, inventoryView, inventoryView.getTopInventory());
public InventoryCloseEvent(InventoryView inventoryView) {
super(inventoryView, inventoryView.getTopInventory());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@
import com.loohp.limbo.events.Cancellable;
import com.loohp.limbo.inventory.InventoryView;
import com.loohp.limbo.inventory.ItemStack;
import com.loohp.limbo.player.Player;
import com.loohp.limbo.player.PlayerInventory;

public class InventoryCreativeEvent extends InventoryEvent implements Cancellable {

private boolean cancelled;
private final int slot;
private ItemStack newItem;

public InventoryCreativeEvent(Player player, InventoryView inventoryView, PlayerInventory playerInventory, int slot, ItemStack newItem) {
super(player, inventoryView, playerInventory);
public InventoryCreativeEvent(InventoryView inventoryView, int slot, ItemStack newItem) {
super(inventoryView, inventoryView.getBottomInventory());
this.slot = slot;
this.newItem = newItem;
this.cancelled = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* This file is part of Limbo.
*
* Copyright (C) 2022. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2022. Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.loohp.limbo.events.inventory;

import com.loohp.limbo.events.Cancellable;
import com.loohp.limbo.inventory.DragType;
import com.loohp.limbo.inventory.InventoryView;
import com.loohp.limbo.inventory.ItemStack;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class InventoryDragEvent extends InventoryEvent implements Cancellable {

private boolean cancelled;
private final DragType type;
private final Map<Integer, ItemStack> addedItems;
private final Set<Integer> containerSlots;
private final ItemStack oldCarried;
private ItemStack newCarried;

public InventoryDragEvent(InventoryView view, ItemStack newCarried, ItemStack oldCarried, boolean right, Map<Integer, ItemStack> slots) {
super(view, view.getInventory(view.convertSlot(slots.keySet().iterator().next())));
this.type = right ? DragType.SINGLE : DragType.EVEN;
this.newCarried = newCarried;
this.oldCarried = oldCarried;
this.addedItems = Collections.unmodifiableMap(slots);
Set<Integer> containerSlots = new HashSet<>();
for (Integer slot : slots.keySet()) {
containerSlots.add(view.convertSlot(slot));
}
this.containerSlots = Collections.unmodifiableSet(containerSlots);
this.cancelled = false;
}

public Map<Integer, ItemStack> getNewItems() {
return addedItems;
}

public Set<Integer> getRawSlots() {
return addedItems.keySet();
}

public Set<Integer> getInventorySlots() {
return containerSlots;
}

public ItemStack get() {
return newCarried;
}

public ItemStack getCarriedItem() {
return newCarried;
}

public void setCarriedItem(ItemStack newCursor) {
this.newCarried = newCursor;
}

public ItemStack getOldCarriedItem() {
return oldCarried.clone();
}

public DragType getType() {
return type;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,20 @@
import com.loohp.limbo.player.Player;

public class InventoryEvent extends Event {

private final Player player;

private final InventoryView inventoryView;
private final Inventory clickedInventory;

public InventoryEvent(Player player, InventoryView inventoryView, Inventory clickedInventory) {
this.player = player;
public InventoryEvent(InventoryView inventoryView, Inventory clickedInventory) {
this.inventoryView = inventoryView;
this.clickedInventory = clickedInventory;
}

public Player getPlayer() {
return player;
return inventoryView.getPlayer();
}

public InventoryView getInventoryView() {
public InventoryView getView() {
return inventoryView;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@

import com.loohp.limbo.events.Cancellable;
import com.loohp.limbo.inventory.InventoryView;
import com.loohp.limbo.player.Player;

public class InventoryOpenEvent extends InventoryEvent implements Cancellable {

private boolean cancelled;

public InventoryOpenEvent(Player player, InventoryView inventoryView) {
super(player, inventoryView, inventoryView.getTopInventory());
public InventoryOpenEvent(InventoryView inventoryView) {
super(inventoryView, inventoryView.getTopInventory());
this.cancelled = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@

public class PlayerSelectedSlotChangeEvent extends PlayerEvent implements Cancellable {

private boolean cancel = false;
private boolean cancelled;
private byte slot;

public PlayerSelectedSlotChangeEvent(Player player, byte slot) {
super(player);
this.slot = slot;
this.cancelled = false;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancel = cancelled;
this.cancelled = cancelled;
}

@Override
public boolean isCancelled() {
return cancel;
return cancelled;
}

public byte getSlot() {
Expand Down
Loading

0 comments on commit 35c6b74

Please sign in to comment.