Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Swofty-Developments committed Dec 31, 2024
2 parents 4c15acd + 8971a33 commit 44d1329
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 3 deletions.
3 changes: 3 additions & 0 deletions configuration/items/communitycenter/communitycenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ items:
- id: BUILDERS_WAND
rarity: COMMON
components:
- id: ABILITY
abilities:
- GRAND_ARCHITECT
- id: ENCHANTED
- id: NOT_FINISHED_YET
- id: DITTO_BLOB
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.swofty.types.generic.event.actions.item;

import lombok.SneakyThrows;
import net.minestom.server.event.player.PlayerBlockBreakEvent;
import net.minestom.server.event.player.PlayerBlockInteractEvent;
import net.minestom.server.item.ItemStack;
import net.swofty.types.generic.event.EventNodes;
import net.swofty.types.generic.event.SkyBlockEvent;
import net.swofty.types.generic.event.SkyBlockEventClass;
import net.swofty.types.generic.item.SkyBlockItem;
import net.swofty.types.generic.item.components.AbilityComponent;
import net.swofty.types.generic.item.handlers.ability.RegisteredAbility;
import net.swofty.types.generic.user.PlayerAbilityHandler;
import net.swofty.types.generic.user.SkyBlockPlayer;

public class ActionItemAbilityBlockLeftUse implements SkyBlockEventClass {
@SneakyThrows
@SkyBlockEvent(node = EventNodes.PLAYER , requireDataLoaded = true)
public void run(PlayerBlockBreakEvent event) {
ItemStack itemStack = event.getPlayer().getItemInMainHand();
SkyBlockItem item = new SkyBlockItem(itemStack);
SkyBlockPlayer player = (SkyBlockPlayer) event.getPlayer();

if (item.hasComponent(AbilityComponent.class)) {
AbilityComponent abilityComponent = item.getComponent(AbilityComponent.class);
RegisteredAbility ability;
ability = abilityComponent.getAbility(RegisteredAbility.AbilityActivation.LEFT_CLICK_BLOCK);
if (ability != null) {
// Cancelling here will prevent the block from being broken
event.setCancelled(true);
if (!ability.getCost().canUse(player)) {
ability.getCost().onFail(player);
return;
}

PlayerAbilityHandler abilityHandler = player.getAbilityHandler();
if (!abilityHandler.canUseAbility(item, ability.getCooldownTicks())) {
player.sendMessage("§cThis ability is on cooldown for " +
Math.round((float) abilityHandler.getRemainingCooldown(item, ability.getCooldownTicks()) / 1000) + "s.");
return;
}

abilityHandler.startAbilityCooldown(item);
ability.execute(player, item, event.getBlockPosition(), event.getBlockFace());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.swofty.types.generic.event.actions.item;

import lombok.SneakyThrows;
import net.minestom.server.event.player.PlayerBlockInteractEvent;
import net.minestom.server.item.ItemStack;
import net.swofty.types.generic.event.EventNodes;
import net.swofty.types.generic.event.SkyBlockEvent;
import net.swofty.types.generic.event.SkyBlockEventClass;
import net.swofty.types.generic.item.SkyBlockItem;
import net.swofty.types.generic.item.components.AbilityComponent;
import net.swofty.types.generic.item.handlers.ability.RegisteredAbility;
import net.swofty.types.generic.user.PlayerAbilityHandler;
import net.swofty.types.generic.user.SkyBlockPlayer;

public class ActionItemAbilityBlockRightUse implements SkyBlockEventClass {
@SneakyThrows
@SkyBlockEvent(node = EventNodes.PLAYER , requireDataLoaded = true)
public void run(PlayerBlockInteractEvent event) {
ItemStack itemStack = event.getPlayer().getItemInMainHand();
SkyBlockItem item = new SkyBlockItem(itemStack);
SkyBlockPlayer player = (SkyBlockPlayer) event.getPlayer();

if (item.hasComponent(AbilityComponent.class)) {
AbilityComponent abilityComponent = item.getComponent(AbilityComponent.class);
RegisteredAbility ability;
ability = abilityComponent.getAbility(RegisteredAbility.AbilityActivation.RIGHT_CLICK_BLOCK);
if (ability != null) {
if (!ability.getCost().canUse(player)) {
ability.getCost().onFail(player);
return;
}

PlayerAbilityHandler abilityHandler = player.getAbilityHandler();
if (!abilityHandler.canUseAbility(item, ability.getCooldownTicks())) {
player.sendMessage("§cThis ability is on cooldown for " +
Math.round((float) abilityHandler.getRemainingCooldown(item, ability.getCooldownTicks()) / 1000) + "s.");
return;
}

abilityHandler.startAbilityCooldown(item);
ability.execute(player, item, event.getBlockPosition(), event.getBlockFace());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.swofty.types.generic.item.handlers.ability;

import net.swofty.types.generic.item.handlers.ability.abilities.BuildersWandAbility;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -15,10 +17,12 @@
RegisteredAbility.AbilityActivation.RIGHT_CLICK,
50,
new RegisteredAbility.AbilityManaCost(25),
(player, item) -> {
(player, item, ignored, ignored2) -> {
player.sendMessage("Hey");
}
));

register(new BuildersWandAbility());
}

public static void register(RegisteredAbility ability) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.Getter;
import lombok.NonNull;
import net.minestom.server.coordinate.Point;
import net.minestom.server.instance.block.BlockFace;
import net.swofty.types.generic.item.SkyBlockItem;
import net.swofty.types.generic.user.SkyBlockActionBar;
import net.swofty.types.generic.user.SkyBlockPlayer;
Expand Down Expand Up @@ -31,24 +33,30 @@ public RegisteredAbility(String id, String name, String description,
}

public void execute(SkyBlockPlayer player, SkyBlockItem item) {
execute(player, item, null, null);
}

public void execute(SkyBlockPlayer player, SkyBlockItem item, Point targetedBlock, BlockFace blockFace) {
if (!cost.canUse(player)) {
cost.onFail(player);
return;
}

cost.onUse(player, this);
action.execute(player, item);
action.execute(player, item, targetedBlock, blockFace);
}

@FunctionalInterface
public interface AbilityAction {
void execute(SkyBlockPlayer player, SkyBlockItem item);
void execute(SkyBlockPlayer player, SkyBlockItem item, Point targetedBlock, BlockFace blockFace);
}

@Getter
public enum AbilityActivation {
RIGHT_CLICK("RIGHT CLICK"),
LEFT_CLICK("LEFT CLICK"),
LEFT_CLICK_BLOCK("LEFT CLICK"),
RIGHT_CLICK_BLOCK("RIGHT CLICK"),
;

private final @NonNull String display;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package net.swofty.types.generic.item.handlers.ability.abilities;

import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.item.Material;
import net.minestom.server.utils.NamespaceID;
import net.swofty.commons.item.ItemType;
import net.swofty.types.generic.item.handlers.ability.RegisteredAbility;
import net.swofty.types.generic.user.SkyBlockPlayer;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class BuildersWandAbility extends RegisteredAbility {

@SuppressWarnings("preview")
public BuildersWandAbility() {
super("GRAND_ARCHITECT", "Grand Architect",
"Right-click the face of a block to extend all connected block faces.",
AbilityActivation.RIGHT_CLICK_BLOCK, 0, new RegisteredAbility.NoAbilityCost(),
(player, item, origin, face) -> {
fillConnectedFaces(player, new Pos(origin), face);
});
}

private static void fillConnectedFaces(SkyBlockPlayer player, Pos origin, BlockFace face) {
Material fillMaterial = Material.fromNamespaceId(player.getInstance().getBlock(origin).namespace());
int blocksInInventory = player.countItem(ItemType.fromMaterial(fillMaterial));
int blockLimit = 164;
if (blocksInInventory < blockLimit)
blockLimit = blocksInInventory;

List<Pos> blocks = new ArrayList<>();
List<Pos> blocksForUndo = new ArrayList<>(); // NOTE: To be used later
blocks.add(origin);
Instance w = player.getInstance();
Vec[] check = null;
Vec translate = null;
int blocksPlaced = 0;

check = switch (face) {
case NORTH, SOUTH ->
new Vec[]{new Vec(-1, -1, 0), new Vec(-1, 0, 0), new Vec(-1, 1, 0), new Vec(0, -1, 0), new Vec(0, 1, 0), new Vec(1, -1, 0), new Vec(1, 0, 0), new Vec(1, 1, 0)};
case EAST, WEST ->
new Vec[]{new Vec(0, -1, -1), new Vec(0, -1, 0), new Vec(0, -1, 1), new Vec(0, 0, -1), new Vec(0, 0, 1), new Vec(0, 1, -1), new Vec(0, 1, 0), new Vec(0, 1, 1)};
case TOP, BOTTOM ->
new Vec[]{new Vec(-1, 0, -1), new Vec(-1, 0, 0), new Vec(-1, 0, 1), new Vec(0, 0, -1), new Vec(0, 0, 1), new Vec(1, 0, -1), new Vec(1, 0, 0), new Vec(1, 0, 1)};
};
translate = switch (face) {
case NORTH -> new Vec(0, 0, -1);
case SOUTH -> new Vec(0, 0, 1);
case EAST -> new Vec(1, 0, 0);
case WEST -> new Vec(-1, 0, 0);
case TOP -> new Vec(0, 1, 0);
case BOTTOM -> new Vec(0, -1, 0);
};
while (!blocks.isEmpty() && blockLimit > 0) {
Pos l = (blocks.get(0));
for (Vec vector : check) {
if (w.getBlock(l.add(vector)).namespace().equals(fillMaterial.namespace()) && w
.getBlock(l.add(vector).add(translate)).namespace().equals(Material.AIR.namespace())) {
blocks.add(l.add(vector));
}
}
Pos fillBlock = l.add(translate);
// TODO: make sure player is on island
if (player.isOnIsland()) {
blocks.removeIf(blocks.get(0)::equals);
if (!player.getInstance().getBlock(fillBlock).namespace().equals(fillMaterial.namespace())) {
player.getInstance().setBlock(fillBlock, Block.fromNamespaceId(fillMaterial.namespace()));

blockLimit--;
blocksPlaced++;
blocksForUndo.add(fillBlock);
}
if (blocksPlaced == blockLimit)
break;
continue;
}
blocks.removeIf(blocks.get(0)::equals);
blockLimit--;
}
if(blocksPlaced == 0) {
player.sendMessage("§cYou cannot place any blocks! You do not have enough blocks to place with your Builder's wand!");
}
if (blocksPlaced != 0) {
player.takeItem(ItemType.fromMaterial(fillMaterial), blocksPlaced);
player.sendMessage("&eYou built &a" + blocksPlaced + "&e blocks!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -405,6 +406,12 @@ public void addAndUpdateItem(ItemStack item) {
addAndUpdateItem(new SkyBlockItem(item));
}

public int countItem(ItemType item) {
AtomicInteger count = new AtomicInteger();
getAllOfTypeInInventory(item).forEach((stack, meow) -> count.addAndGet(meow));
return count.get();
}

public boolean takeItem(SkyBlockItem itemToTake) {
for (int i = 0; i < 36; i++) {
ItemStack stack = getInventory().getItemStack(i);
Expand Down

0 comments on commit 44d1329

Please sign in to comment.