-
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.
- Loading branch information
1 parent
527172e
commit d99b136
Showing
23 changed files
with
731 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,5 @@ build/ | |
### NixOS ### | ||
.envrc | ||
.env | ||
|
||
worlds/ |
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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/slampvp/factory/command/minion/MinionCommand.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,22 @@ | ||
package com.slampvp.factory.command.minion; | ||
|
||
import com.slampvp.factory.command.Command; | ||
import com.slampvp.factory.command.FactoryCommand; | ||
import com.slampvp.factory.minion.models.Minion; | ||
import com.slampvp.factory.player.Rank; | ||
|
||
@Command(description = "Minion command.", usage = "/minion", minimumRank = Rank.ADMIN, playerOnly = false) | ||
public class MinionCommand extends FactoryCommand { | ||
public MinionCommand() { | ||
super("minion"); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
addSyntax(((sender, context) -> { | ||
Minion.all().forEach(minion -> { | ||
sender.sendMessage(minion.toString()); | ||
}); | ||
})); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/com/slampvp/factory/command/minion/sub/GiveCommand.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,89 @@ | ||
package com.slampvp.factory.command.minion.sub; | ||
|
||
import com.slampvp.factory.command.Command; | ||
import com.slampvp.factory.command.FactoryCommand; | ||
import com.slampvp.factory.common.Locale; | ||
import com.slampvp.factory.common.StringUtil; | ||
import com.slampvp.factory.minion.models.Minion; | ||
import com.slampvp.factory.minion.models.Minions; | ||
import com.slampvp.factory.player.Rank; | ||
import net.minestom.server.command.builder.arguments.ArgumentString; | ||
import net.minestom.server.command.builder.arguments.ArgumentType; | ||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentEntity; | ||
import net.minestom.server.command.builder.arguments.number.ArgumentInteger; | ||
import net.minestom.server.command.builder.arguments.number.ArgumentNumber; | ||
import net.minestom.server.command.builder.suggestion.SuggestionEntry; | ||
import net.minestom.server.entity.Player; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
|
||
@Command( | ||
description = "Give a minion to a player.", | ||
usage = "/minion give <player> <minion> [amount]", | ||
minimumRank = Rank.ADMIN, | ||
playerOnly = false | ||
) | ||
public class GiveCommand extends FactoryCommand { | ||
public GiveCommand() { | ||
super("give"); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
ArgumentEntity argumentPlayer = ArgumentType.Entity("player").singleEntity(true).onlyPlayers(true); | ||
ArgumentString argumentMinion = ArgumentType.String("minion"); | ||
ArgumentNumber<Integer> argumentAmount = ArgumentType.Integer("amount").min(1); | ||
|
||
argumentMinion.setSuggestionCallback((sender, context, suggestion) -> { | ||
String arg = suggestion.getInput().substring(suggestion.getStart() - 1, suggestion.getStart() + suggestion.getLength() - 1); | ||
StringUtil.copyPartialMatches(arg, Minion.all().stream().map(Minion::id).toList(), new ArrayList<>()).forEach(string -> { | ||
suggestion.addEntry(new SuggestionEntry(string)); | ||
}); | ||
}); | ||
|
||
addSyntax((sender, context) -> { | ||
Player target = context.get(argumentPlayer).findFirstPlayer(sender); | ||
|
||
if (target == null) { | ||
sender.sendMessage(Locale.Command.INVALID_PLAYER); | ||
return; | ||
} | ||
|
||
Optional<Minion> optionalMinion = Minion.byId(context.get(argumentMinion)); | ||
|
||
if (optionalMinion.isEmpty()) { | ||
sender.sendMessage(Locale.Minion.INVALID_ID); | ||
return; | ||
} | ||
|
||
Minion minion = optionalMinion.get(); | ||
|
||
target.getInventory().addItemStack(minion.getItem()); | ||
}, argumentPlayer, argumentMinion); | ||
|
||
addSyntax((sender, context) -> { | ||
Player target = context.get(argumentPlayer).findFirstPlayer(sender); | ||
|
||
if (target == null) { | ||
sender.sendMessage(Locale.Command.INVALID_PLAYER); | ||
return; | ||
} | ||
|
||
Optional<Minion> optionalMinion = Minion.byId(context.get(argumentMinion)); | ||
|
||
if (optionalMinion.isEmpty()) { | ||
sender.sendMessage(Locale.Minion.INVALID_ID); | ||
return; | ||
} | ||
|
||
int amount = context.get(argumentAmount); | ||
|
||
Minion minion = optionalMinion.get(); | ||
|
||
for (int i = 0; i < amount; i++) { | ||
target.getInventory().addItemStack(minion.getItem()); | ||
} | ||
}, argumentPlayer, argumentMinion, argumentAmount); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/slampvp/factory/common/ItemStackUtil.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,17 @@ | ||
package com.slampvp.factory.common; | ||
|
||
import net.minestom.server.entity.PlayerSkin; | ||
import net.minestom.server.item.ItemComponent; | ||
import net.minestom.server.item.ItemStack; | ||
import net.minestom.server.item.Material; | ||
import net.minestom.server.item.component.HeadProfile; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class ItemStackUtil { | ||
public static @NotNull ItemStack.Builder texturedHead(String texture) { | ||
ItemStack.Builder skull = ItemStack.builder(Material.PLAYER_HEAD); | ||
HeadProfile profile = new HeadProfile(new PlayerSkin(texture, null)); | ||
skull.set(ItemComponent.PROFILE, profile); | ||
return skull; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.slampvp.factory.common; | ||
|
||
import java.util.Collection; | ||
|
||
public class StringUtil { | ||
public static <T extends Collection<? super String>> T copyPartialMatches(String token, Iterable<String> originals, T collection) | ||
throws UnsupportedOperationException, IllegalArgumentException { | ||
if (token.length() == 1 && Integer.toHexString(token.charAt(0) | 0x10000).substring(1).equals("0000")) { | ||
token = ""; | ||
} | ||
|
||
for (String string : originals) { | ||
if (startsWithIgnoreCase(string, token)) { | ||
collection.add(string); | ||
} | ||
} | ||
return collection; | ||
} | ||
|
||
private static boolean startsWithIgnoreCase(String string, String prefix) throws IllegalArgumentException, NullPointerException { | ||
return string.length() >= prefix.length() && string.regionMatches(true, 0, prefix, 0, prefix.length()); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/slampvp/factory/database/queries/MinionQueries.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,23 @@ | ||
package com.slampvp.factory.database.queries; | ||
|
||
import org.intellij.lang.annotations.Language; | ||
|
||
public final class MinionQueries { | ||
public static final class Insert { | ||
@Language("PostgreSQL") | ||
public static final String MINION = """ | ||
INSERT INTO minions (minion_id, owner, time_active, amount_generated, position, chest_position) | ||
VALUES (?,?,?,?,?,?); | ||
"""; | ||
} | ||
|
||
public static final class Select { | ||
@Language("PostgreSQL") | ||
public static final String BY_OWNER = "SELECT * FROM minions WHERE owner = ?"; | ||
} | ||
|
||
public static final class Delete { | ||
@Language("PostgreSQL") | ||
public static final String BY_ID = "DELETE FROM plots WHERE id = ?"; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/slampvp/factory/minion/MinionListener.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,37 @@ | ||
package com.slampvp.factory.minion; | ||
|
||
import com.slampvp.factory.minion.models.Minion; | ||
import net.minestom.server.MinecraftServer; | ||
import net.minestom.server.entity.Player; | ||
import net.minestom.server.event.GlobalEventHandler; | ||
import net.minestom.server.event.player.PlayerBlockPlaceEvent; | ||
import net.minestom.server.item.ItemStack; | ||
import net.minestom.server.tag.Tag; | ||
|
||
import java.util.Optional; | ||
|
||
public class MinionListener { | ||
public MinionListener() { | ||
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler(); | ||
MinionManager minionManager = MinionManager.getInstance(); | ||
|
||
globalEventHandler.addListener(PlayerBlockPlaceEvent.class, event -> { | ||
Player player = event.getPlayer(); | ||
ItemStack itemInHand = player.getItemInHand(event.getHand()); | ||
|
||
String minionId = itemInHand.getTag(Tag.String("minion")); | ||
if (minionId == null) { | ||
return; | ||
} | ||
|
||
Optional<Minion> optionalMinion = Minion.byId(minionId); | ||
if (optionalMinion.isEmpty()) { | ||
return; | ||
} | ||
|
||
event.setCancelled(true); | ||
player.setItemInMainHand(itemInHand.withAmount(itemInHand.amount() - 1)); | ||
minionManager.addMinion(player, event.getBlockPosition().asVec(), optionalMinion.get()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.