Improvements to ChestGui system.

This commit is contained in:
Olof Larsson 2016-06-15 12:25:47 +02:00
parent 5aa6cc730d
commit fdc7935d44
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
3 changed files with 29 additions and 7 deletions

View File

@ -28,10 +28,7 @@ public final class SoundEffect implements Serializable
@EditorType(TypeSoundId.class)
private final String soundId;
public String getSoundId() { return this.soundId; }
public Sound getSound()
{
return TypeSound.valueOf(this.getSoundId());
}
public Sound getSound() { return TypeSound.valueOf(this.getSoundId()); }
private final float volume;
public float getVolume() { return this.volume; }

View File

@ -17,7 +17,7 @@ public class ChestGui
// REGISTRY
// -------------------------------------------- //
private static final Map<Inventory, ChestGui> inventoryToGui = new MassiveMap<Inventory, ChestGui>();
private static final Map<Inventory, ChestGui> inventoryToGui = new MassiveMap<>();
public static Map<Inventory, ChestGui> getInventoryToGui() { return inventoryToGui; }
public static ChestGui remove(Inventory inventory) { return inventoryToGui.remove(inventory); }
public static ChestGui set(Inventory inventory, ChestGui gui) { return inventoryToGui.put(inventory, gui); }
@ -34,6 +34,11 @@ public class ChestGui
// -------------------------------------------- //
// ACTIONS
// -------------------------------------------- //
// Actions are assigned to indexes in the inventory.
// This means the system does not care about what item is in the slot.
// It just cares about which slot it is.
// One could have imagined an approach where we looked at the item instead.
// That is however not feasible since the Bukkit ItemStack equals method is not reliable.
private Map<Integer, ChestAction> indexToAction = new MassiveMap<Integer, ChestAction>();
public Map<Integer, ChestAction> getIndexToAction() { return this.indexToAction; }
@ -45,6 +50,8 @@ public class ChestGui
// -------------------------------------------- //
// LAST ACTION
// -------------------------------------------- //
// The last executed action is stored here.
// This can for example be useful in the inventory close task.
private ChestAction lastAction = null;
public ChestAction getLastAction() { return this.lastAction; }
@ -53,6 +60,8 @@ public class ChestGui
// -------------------------------------------- //
// META DATA
// -------------------------------------------- //
// Store your arbitrary stuff here. Might come in handy in the future.
// I don't think we are currently using this ourselves.
private final Map<String, Object> meta = new MassiveMap<>();
public Map<String, Object> getMeta() { return this.meta; }
@ -60,6 +69,9 @@ public class ChestGui
// -------------------------------------------- //
// RUNNABLES
// -------------------------------------------- //
// Runnables to be executed after certain events.
// They are all delayed with one (or is it zero) ticks.
// This way we don't bug out if you open a new GUI after close.
private final List<Runnable> runnablesOpen = new MassiveList<>();
public List<Runnable> getRunnablesOpen() { return this.runnablesOpen; }
@ -70,11 +82,22 @@ public class ChestGui
// -------------------------------------------- //
// SOUND
// -------------------------------------------- //
// The click sound you should hear when clicking an action slot.
// You can disable it by setting null.
private SoundEffect soundEffect = MassiveCoreMConf.get().clickSound;
public SoundEffect getSoundEffect() { return this.soundEffect; }
public ChestGui setSoundEffect(SoundEffect soundEffect) { this.soundEffect = soundEffect; return this; }
// -------------------------------------------- //
// AUTOCLOSING
// -------------------------------------------- //
// Should the GUI be automatically closed upon clicking an action?
private boolean autoclosing = true;
public boolean isAutoclosing() { return this.autoclosing; }
public ChestGui setAutoclosing(boolean autoclosing) { this.autoclosing = autoclosing; return this; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //

View File

@ -9,6 +9,7 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.Inventory;
import com.massivecraft.massivecore.Engine;
import com.massivecraft.massivecore.SoundEffect;
import com.massivecraft.massivecore.chestgui.ChestAction;
import com.massivecraft.massivecore.chestgui.ChestGui;
import com.massivecraft.massivecore.mixin.MixinMessage;
@ -60,10 +61,11 @@ public class EngineMassiveCoreChestGui extends Engine
gui.setLastAction(action);
// ... then play the sound ...
gui.getSoundEffect().run(event.getWhoClicked());
SoundEffect soundEffect = gui.getSoundEffect();
if (soundEffect != null) soundEffect.run(event.getWhoClicked());
// ... close the GUI ...
event.getView().close();
if (gui.isAutoclosing()) event.getView().close();
// ... and use that action.
action.onClick(event);