-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gemstone and gemstone slots component to Pojo Expansion
- Loading branch information
Showing
21 changed files
with
1,034 additions
and
167 deletions.
There are no files selected for viewing
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
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
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
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
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
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
68 changes: 68 additions & 0 deletions
68
...ansion/src/main/java/io/github/nahkd123/pojo/expansion/event/InventoryEventsListener.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,68 @@ | ||
package io.github.nahkd123.pojo.expansion.event; | ||
|
||
import java.util.Optional; | ||
|
||
import org.bukkit.Sound; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.inventory.InventoryClickEvent; | ||
import org.bukkit.event.inventory.InventoryType; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
|
||
import io.github.nahkd123.pojo.api.item.PojoItem; | ||
import io.github.nahkd123.pojo.api.item.standard.StandardPojoItem; | ||
import io.github.nahkd123.pojo.api.item.standard.component.ComponentDataHolder; | ||
import io.github.nahkd123.pojo.expansion.item.standard.GemstoneComponent; | ||
import io.github.nahkd123.pojo.expansion.item.standard.GemstoneSlotsComponent; | ||
import io.github.nahkd123.pojo.expansion.stat.gemstone.Gemstone; | ||
import io.github.nahkd123.pojo.expansion.stat.gemstone.GemstonesHolder; | ||
|
||
public class InventoryEventsListener implements Listener { | ||
@EventHandler | ||
public void onInventoryClick(InventoryClickEvent event) { | ||
if (event.isCancelled()) return; | ||
if (event.getInventory().getType() != InventoryType.CRAFTING) return; | ||
if (checkGemstoneApply(event)) return; | ||
} | ||
|
||
private boolean checkGemstoneApply(InventoryClickEvent event) { | ||
ItemStack cursorStack = event.getCursor(); | ||
if (!(PojoItem.getFrom(cursorStack) instanceof StandardPojoItem cursorStd)) return false; | ||
|
||
ItemStack clickedStack = event.getCurrentItem(); | ||
if (!(PojoItem.getFrom(clickedStack) instanceof StandardPojoItem clickedStd)) return false; | ||
|
||
Optional<GemstoneComponent> gemstoneComponent = cursorStd.getComponents().stream() | ||
.filter(s -> s instanceof GemstoneComponent) | ||
.findAny().map(s -> (GemstoneComponent) s); | ||
if (gemstoneComponent.isEmpty()) return false; | ||
|
||
Optional<GemstoneSlotsComponent> gemstoneSlotComponent = clickedStd.getComponents().stream() | ||
.filter(s -> s instanceof GemstoneSlotsComponent) | ||
.findAny().map(s -> (GemstoneSlotsComponent) s); | ||
if (gemstoneSlotComponent.isEmpty()) return false; | ||
|
||
// Test | ||
ComponentDataHolder clickedDataHolder = clickedStd.loadDataFrom(clickedStack.getItemMeta(), true); | ||
GemstonesHolder slots = clickedDataHolder.get(gemstoneSlotComponent.get()); | ||
|
||
ComponentDataHolder cursorDataHolder = cursorStd.loadDataFrom(cursorStack.getItemMeta(), true); | ||
Gemstone gemstone = new Gemstone(PojoItem.getId(cursorStack), cursorDataHolder); | ||
if (!slots.tryAttachGemstone(gemstoneComponent.get().getSlotId(), gemstone)) return false; | ||
|
||
// Apply | ||
clickedStack = clickedStack.clone(); | ||
ItemMeta meta = clickedStack.getItemMeta(); | ||
clickedStd.saveDataTo(meta.getPersistentDataContainer(), clickedDataHolder); | ||
clickedStd.updateMeta(meta); | ||
clickedStack.setItemMeta(meta); | ||
event.setCancelled(true); | ||
event.setCurrentItem(clickedStack); | ||
event.getWhoClicked().setItemOnCursor(null); | ||
if (event.getWhoClicked() instanceof Player p) | ||
p.playSound(p.getEyeLocation(), Sound.BLOCK_SMITHING_TABLE_USE, 1f, 1f); | ||
return true; | ||
} | ||
} |
Oops, something went wrong.