Skip to content

Commit

Permalink
Fixed "random" for ShopKeeperSkins and some other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Jun 15, 2023
1 parent c7eb47c commit c4db043
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 83 deletions.
17 changes: 15 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.defender</groupId>
<artifactId>BW1058-Cosmetics</artifactId>
<version>1.4.2</version>
<version>1.4.4</version>
<packaging>jar</packaging>

<name>Cosmetics</name>
Expand Down Expand Up @@ -45,6 +45,18 @@
<shadedPattern>me.defender.cosmetics.support.hcore</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com/cryptomorin/xseries/XBiome*</exclude>
<exclude>com/cryptomorin/xseries/NMSExtras*</exclude>
<exclude>com/cryptomorin/xseries/NoteBlockMusic*</exclude>
<exclude>com/cryptomorin/xseries/SkullCacheListener*</exclude>
<exclude>com/hakan/core/scoreboard</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
Expand Down Expand Up @@ -83,6 +95,7 @@
<id>enginehub-maven</id>
<url>https://maven.enginehub.org/repo/</url>
</repository>

</repositories>

<dependencies>
Expand All @@ -96,7 +109,7 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>compile</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldedit</groupId>
Expand Down
24 changes: 9 additions & 15 deletions src/main/java/me/defender/cosmetics/Cosmetics.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,9 @@ public class Cosmetics extends JavaPlugin
public static HikariDataSource db;
@Getter
public static Connection dbConnection;
public boolean forcedDisable = false;
public boolean dependenciesMissing = false;
static boolean placeholderAPI;

@Override
public void onEnable() {
loadOnEnable();
}


public void onDisable() {
unloadOnDisable();
}

public static void downloadFile(URL url, String filePath) {
try {
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
Expand All @@ -59,11 +49,12 @@ public static void downloadFile(URL url, String filePath) {
}
}

private void loadOnEnable() {
@Override
public void onEnable() {
if(!StartupUtils.checkDependencies()){
getLogger().severe("Cosmetics addon will now disable, make sure you have all dependencies installed!");
getServer().getPluginManager().disablePlugin(this);
forcedDisable = true;
dependenciesMissing = true;
return;
}

Expand Down Expand Up @@ -121,11 +112,14 @@ private void loadOnEnable() {
getLogger().info("Loading cosmetics...");
StartupUtils.loadCosmetics();
getLogger().info("Addon have been loaded and enabled!");
// This is a check to make sure victory dance config doesn't have any issues.
VictoryDance.getDefault(null);

}

private void unloadOnDisable() {
if(forcedDisable){
@Override
public void onDisable() {
if(dependenciesMissing){
getLogger().severe("Detected forced disable! plugin will not unload anything!");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

package me.defender.cosmetics.api.category.islandtoppers.util;

import com.andrei1058.bedwars.api.arena.IArena;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.schematic.SchematicFormat;
import me.defender.cosmetics.api.BwcAPI;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.material.Bed;
import org.bukkit.material.Directional;

import java.io.File;
import java.io.IOException;
Expand All @@ -26,14 +32,40 @@ public static void sendIslandTopper(World world, Location loc, Player p, File fi
Bukkit.getLogger().severe("Schematic format is null! most probably file is invalid! (" + file.getName() + ")");
return;
}
CuboidClipboard clipboard = schematicFormat.load(file);
// Move the schematic to the target location
clipboard.setOrigin(new Vector(loc.getX(), loc.getY() + clipboard.getHeight() + 1, loc.getZ()));
// Paste the schematic
clipboard.paste(editSession, new Vector(loc.getX(), loc.getY() + clipboard.getHeight() + 1, loc.getZ()), true);
IArena arena = new BwcAPI().getBwAPI().getArenaUtil().getArenaByPlayer(p);
if(arena != null) {
Block block = arena.getTeam(p).getBed().getBlock();
if(block.getState() instanceof Bed && block.getState().getData() instanceof Directional){
Directional directional = (Directional) block.getState().getData();
CuboidClipboard clipboard = schematicFormat.load(file);
clipboard = flipDirection(clipboard, directional.getFacing());
// Move the schematic to the target location
clipboard.setOrigin(new Vector(loc.getX(), loc.getY() + clipboard.getHeight() + 1, loc.getZ()));
// Paste the schematic
clipboard.paste(editSession, new Vector(loc.getX(), loc.getY() + clipboard.getHeight() + 1, loc.getZ()), true);

}
}
} catch (Exception e) {
e.printStackTrace();
}

}


private static CuboidClipboard flipDirection(CuboidClipboard clipboard, BlockFace direction) {
switch (direction) {
case NORTH:
clipboard.rotate2D(180);
return clipboard;
case EAST:
clipboard.rotate2D(270);
return clipboard;
case WEST:
clipboard.rotate2D(90);
return clipboard;
default:
return clipboard;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
import me.defender.cosmetics.api.util.DebugUtil;
import me.defender.cosmetics.api.util.Utility;
import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.Debug;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -50,16 +53,16 @@ public void run() {
Location shopLocation = team.getShop();
Location upgradeLocation = team.getTeamUpgrades();
World world = shopLocation.getWorld();

DebugUtil.addMessage("Executing ShopKeeper Skins for team " + team.getName());
// Delete existing NPCs
world.getEntities().stream()
.filter(e -> (e.getLocation().distance(shopLocation) <= 0.2 || e.getLocation().distance(upgradeLocation) <= 0.2))
.filter(e -> (e.getLocation().distance(shopLocation) <= 0.2 || e.getLocation().distance(upgradeLocation) <= 0.2 && e instanceof Villager))
.forEach(Entity::remove);

// Choose random player from the team
Player player = team.getMembers().get(new Random().nextInt(team.getSize()));
String skin = new BwcAPI().getSelectedCosmetic(player, CosmeticsType.ShopKeeperSkin);

DebugUtil.addMessage("Selected skin: " + skin);
// Spawn new NPCs
for (ShopKeeperSkin skins : StartupUtils.shopKeeperSkinList) {
if (skin.equals(skins.getIdentifier())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.defender.cosmetics.api.util.StringUtils;
import me.defender.cosmetics.api.configuration.ConfigManager;
import me.defender.cosmetics.api.category.shopkeeperskins.utils.ShopKeeperSkinsUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -74,8 +75,14 @@ public void execute(Player player, Location shopLocation, Location upgradeLocati
shopKeeperSkins.add(shopKeeperSkin);
}
}
shopKeeperSkins.get(new Random().nextInt(shopKeeperSkins.size())).execute(player, shopLocation, upgradeLocation);
return;
if(shopKeeperSkins.isEmpty()){
// ShopKeeperSkin#getDefault should not return null!
ShopKeeperSkinsUtils.spawnShopKeeperNPC(player, shopLocation, upgradeLocation, ShopKeeperSkin.getDefault(player).getIdentifier());
}else{
ShopKeeperSkin shopKeeperSkin1 = shopKeeperSkins.get(new Random().nextInt(shopKeeperSkins.size()));
ShopKeeperSkinsUtils.spawnShopKeeperNPC(player, shopLocation, upgradeLocation, shopKeeperSkin1.getIdentifier());
}
return;
}
ShopKeeperSkinsUtils.spawnShopKeeperNPC(player, shopLocation, upgradeLocation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hakan.core.HCore;
import com.hakan.core.ui.inventory.InventoryGui;
import com.hakan.core.utils.ColorUtil;
import me.defender.cosmetics.api.category.shopkeeperskins.ShopKeeperSkin;
import me.defender.cosmetics.api.category.shopkeeperskins.utils.ShopKeeperSkinsUtils;
import me.defender.cosmetics.api.enums.FieldsType;
Expand All @@ -26,13 +27,23 @@ public void sendPreviewShopKeeperSkin(Player player, String selected, InventoryG
Location beforeLocation = player.getLocation();
float walkSpeed = player.getWalkSpeed();
player.closeInventory();
Location cosmeticLocation = null, playerLocation = null;

Location cosmeticLocation = getCosmeticLocation();
Location playerLocation = getPlayerLocation();
try {
cosmeticLocation = getCosmeticLocation();
playerLocation = getPlayerLocation();
}catch (Exception exception){
exception.printStackTrace();
player.sendMessage(ColorUtil.colored("&cEither Preview location or Player location is not set! Contact the admin."));
}

if(cosmeticLocation == null || playerLocation == null) return;

HCore.asyncScheduler().run(() -> {
player.teleport(playerLocation);
ShopKeeperSkinsUtils.spawnShopKeeperNPCForPreview(player, cosmeticLocation);
Location finalPlayerLocation = playerLocation;
Location finalCosmeticLocation = cosmeticLocation;
HCore.syncScheduler().run(() -> {
player.teleport(finalPlayerLocation);
ShopKeeperSkinsUtils.spawnShopKeeperNPCForPreview(player, finalCosmeticLocation);
player.setWalkSpeed(0);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.defender.cosmetics.api.category.shopkeeperskins.utils;

import com.hakan.core.HCore;
import com.hakan.core.npc.Npc;
import com.hakan.core.skin.Skin;
import me.defender.cosmetics.Cosmetics;
import me.defender.cosmetics.api.BwcAPI;
import me.defender.cosmetics.api.enums.CosmeticsType;
Expand All @@ -19,10 +22,7 @@
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.*;

import static me.defender.cosmetics.api.util.Utility.plugin;

Expand Down Expand Up @@ -76,25 +76,8 @@ private static void createShopKeeperNPC(Player p, Location loc, Location loc1, S
value = values.get(0);
sign = values.get(1);
}
NPCRegistry registry = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore());
// Shop NPC
NPC npc = registry.createNPC(EntityType.PLAYER, "");
npc.setName("&r");
npc.getTrait(SkinTrait.class).setSkinPersistent(UUID.randomUUID().toString(), sign, value);
npc.getTrait(SkinTrait.class).setTexture(value, sign);
npc.getTrait(LookClose.class).lookClose(true);
npc.getOrAddTrait(HologramTrait.class).clear();
npc.spawn(loc);
npc.getEntity().setMetadata("NPC2", new FixedMetadataValue(plugin(), ""));
// Upgrade NPC
NPC npc1 = registry.createNPC(EntityType.PLAYER, "");
npc1.setName("&r");
npc1.getTrait(SkinTrait.class).setSkinPersistent(UUID.randomUUID().toString(), sign, value);
npc1.getTrait(SkinTrait.class).setTexture(value, sign);
npc1.getTrait(LookClose.class).lookClose(true);
npc.data().setPersistent(NPC.Metadata.NAMEPLATE_VISIBLE, false);
npc1.spawn(loc1);
npc1.getEntity().setMetadata("NPC2", new FixedMetadataValue(plugin(), ""));
Npc npc = HCore.npcBuilder(new Random().nextInt() + "ID").skin(new Skin(value, sign)).showEveryone(true).target(Npc.LookTarget.NEAREST).lines(Collections.emptyList()).location(loc).build();
Npc npc1 = HCore.npcBuilder(new Random().nextInt() + "ID").skin(new Skin(value, sign)).showEveryone(true).target(Npc.LookTarget.NEAREST).lines(Collections.emptyList()).location(loc1).build();
}

/**
Expand All @@ -114,7 +97,7 @@ private static void createShopKeeperNPC(Player p, Location loc, String value, St
npc.setName("&r");
npc.getTrait(SkinTrait.class).setSkinPersistent(UUID.randomUUID().toString(), sign, value);
npc.getTrait(SkinTrait.class).setTexture(value, sign);
// npc.getTrait(LookClose.class).lookClose(true);
npc.getTrait(LookClose.class).lookClose(true);
npc.getOrAddTrait(HologramTrait.class).clear();
npc.spawn(loc);
npc.getEntity().setMetadata("NPC2", new FixedMetadataValue(plugin(), ""));
Expand Down Expand Up @@ -164,6 +147,26 @@ public static void spawnShopKeeperNPC(Player p, Location loc, Location loc1) {
}
}

public static void spawnShopKeeperNPC(Player p, Location loc, Location loc1, String skin) {
ConfigManager config = ConfigUtils.getShopKeeperSkins();
String key = CosmeticsType.ShopKeeperSkin.getSectionKey();
String skinvalue = config.getString(key + "." + skin + ".skin-value");
String skinsign = config.getString(key + "." + skin + ".skin-sign");
String etype = config.getString(key + "." + skin + ".entity-type");
boolean mirror = config.getBoolean(key + "." + skin + ".mirror");

if(mirror){
createShopKeeperNPC(p, loc, loc1, skinvalue, skinsign, true);
return;
}
if(etype != null) {
createEntityNPC(EntityType.valueOf(etype), loc1);
createEntityNPC(EntityType.valueOf(etype), loc);
}else if(skinvalue != null && skinsign != null) {
createShopKeeperNPC(p, loc, loc1, skinvalue, skinsign, false);
}
}

public static void spawnShopKeeperNPCForPreview(Player p, Location loc) {
Cosmetics plugin = plugin();
String skin = new BwcAPI().getSelectedCosmetic(p, CosmeticsType.ShopKeeperSkin);
Expand Down
Loading

0 comments on commit c4db043

Please sign in to comment.