1.1.7
## Added - Update Checker - - Alerts OP user update available if applicable. - - Enters log entry if update available - Warden - Armadillo (All Variants)
This commit is contained in:
1 parent
59ba08c8dd
commit
76fca86759
6 files changed
+263
No files matched your search
@@ -4,6 +4,7 @@ import com.rsscripting.mobcage.item.CageRecipe;
|
||||
import com.rsscripting.mobcage.listener.MobCageListener;
|
||||
import com.rsscripting.mobcage.listener.MobReleaseListener;
|
||||
import com.rsscripting.mobcage.util.Keys;
|
||||
import com.rsscripting.mobcage.util.UpdateChecker;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
@@ -31,6 +32,10 @@ public final class MobCage extends JavaPlugin {
|
||||
this
|
||||
);
|
||||
|
||||
UpdateChecker updateChecker = new UpdateChecker(this);
|
||||
getServer().getPluginManager().registerEvents(updateChecker, this);
|
||||
updateChecker.checkForUpdates();
|
||||
|
||||
getLogger().info(
|
||||
"RS-MobCage Enabled"
|
||||
);
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.rsscripting.mobcage.description.descriptor;
|
||||
|
||||
import com.rsscripting.mobcage.description.CaptureResult;
|
||||
import com.rsscripting.mobcage.description.MobAttribute;
|
||||
import com.rsscripting.mobcage.util.MobAgeUtil;
|
||||
import com.rsscripting.mobcage.util.MobHealthUtil;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Armadillo;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class ArmadilloDescriptor implements MobDescriptor {
|
||||
|
||||
@Override
|
||||
public boolean supports(LivingEntity entity) {
|
||||
return entity instanceof Armadillo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CaptureResult beforeCapture(LivingEntity entity, Player player) {
|
||||
CaptureResult result = MobHealthUtil.validateFullHealth(entity);
|
||||
|
||||
if (!result.isAllowed()) {
|
||||
return result;
|
||||
}
|
||||
|
||||
MobAgeUtil.normalize(entity);
|
||||
|
||||
return CaptureResult.allowed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayName(LivingEntity entity) {
|
||||
Armadillo armadillo = (Armadillo) entity;
|
||||
|
||||
return armadillo.isAdult() ? "Armadillo" : "Baby Armadillo";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MobAttribute> getAttributes(LivingEntity entity) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release(Location location, List<Component> lore) {
|
||||
|
||||
boolean baby = false;
|
||||
|
||||
PlainTextComponentSerializer plain =
|
||||
PlainTextComponentSerializer.plainText();
|
||||
|
||||
for (Component line : lore) {
|
||||
String text = plain.serialize(line);
|
||||
|
||||
if (text.equals("Baby Armadillo")) {
|
||||
baby = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Armadillo armadillo = (Armadillo) location.getWorld().spawnEntity(
|
||||
location,
|
||||
EntityType.ARMADILLO
|
||||
);
|
||||
|
||||
if (baby) {
|
||||
armadillo.setBaby();
|
||||
} else {
|
||||
armadillo.setAdult();
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
@@ -8,6 +8,7 @@ public final class MobDescriptorRegistry {
|
||||
|
||||
private static final List<MobDescriptor> DESCRIPTORS = List.of(
|
||||
new AxolotlDescriptor(),
|
||||
new ArmadilloDescriptor(),
|
||||
new BeeDescriptor(),
|
||||
new CatDescriptor(),
|
||||
new CaveSpiderDescriptor(),
|
||||
@@ -26,6 +27,7 @@ public final class MobDescriptorRegistry {
|
||||
new SlimeDescriptor(),
|
||||
new SpiderDescriptor(),
|
||||
new VillagerDescriptor(),
|
||||
new WardenDescriptor(),
|
||||
new WolfDescriptor(),
|
||||
new ZombieDescriptor()
|
||||
);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.rsscripting.mobcage.description.descriptor;
|
||||
|
||||
import com.rsscripting.mobcage.description.CaptureResult;
|
||||
import com.rsscripting.mobcage.util.MobHealthUtil;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Warden;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class WardenDescriptor implements MobDescriptor {
|
||||
|
||||
@Override
|
||||
public boolean supports(LivingEntity entity) {
|
||||
return entity instanceof Warden;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CaptureResult beforeCapture(LivingEntity entity, Player player) {
|
||||
return MobHealthUtil.validateFullHealth(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayName(LivingEntity entity) {
|
||||
return "Warden";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<com.rsscripting.mobcage.description.MobAttribute> getAttributes(
|
||||
LivingEntity entity
|
||||
) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release(Location location, List<Component> lore) {
|
||||
location.getWorld().spawnEntity(location, EntityType.WARDEN);
|
||||
}
|
||||
}
|
||||
@@ -61,6 +61,9 @@ public class MobReleaseListener implements Listener {
|
||||
|
||||
switch (contains) {
|
||||
|
||||
case "Armadillo", "Baby Armadillo" ->
|
||||
new ArmadilloDescriptor().release(location, lore);
|
||||
|
||||
case "Axolotl", "Baby Axolotl" ->
|
||||
new AxolotlDescriptor().release(location, lore);
|
||||
|
||||
@@ -118,6 +121,9 @@ public class MobReleaseListener implements Listener {
|
||||
case "Villager", "Baby Villager" ->
|
||||
new VillagerDescriptor().release(location, lore);
|
||||
|
||||
case "Warden", "Baby Warden" ->
|
||||
new WardenDescriptor().release(location, lore);
|
||||
|
||||
case "Wolf", "Baby Wolf" ->
|
||||
new WolfDescriptor().release(location, lore);
|
||||
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.rsscripting.mobcage.util;
|
||||
|
||||
import com.rsscripting.mobcage.MobCage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
|
||||
public class UpdateChecker implements Listener {
|
||||
|
||||
private static final String VERSION_URL =
|
||||
"https://raw.githubusercontent.com/russelburgraymond/RS-MobCage/main/pom.xml";
|
||||
|
||||
private final MobCage plugin;
|
||||
private String latestVersion;
|
||||
|
||||
public UpdateChecker(MobCage plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void checkForUpdates() {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
HttpURLConnection connection =
|
||||
(HttpURLConnection) URI.create(VERSION_URL)
|
||||
.toURL()
|
||||
.openConnection();
|
||||
|
||||
connection.setConnectTimeout(5000);
|
||||
connection.setReadTimeout(5000);
|
||||
connection.setRequestMethod("GET");
|
||||
|
||||
try (InputStream input = connection.getInputStream()) {
|
||||
var factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setFeature(
|
||||
"http://apache.org/xml/features/disallow-doctype-decl",
|
||||
true
|
||||
);
|
||||
|
||||
var document = factory.newDocumentBuilder().parse(input);
|
||||
latestVersion = document
|
||||
.getElementsByTagName("version")
|
||||
.item(0)
|
||||
.getTextContent()
|
||||
.trim();
|
||||
|
||||
String currentVersion = plugin.getPluginMeta().getVersion();
|
||||
|
||||
if (isNewerVersion(latestVersion, currentVersion)) {
|
||||
plugin.getLogger().warning(
|
||||
"Update available! Current version: "
|
||||
+ currentVersion
|
||||
+ " | Latest version: "
|
||||
+ latestVersion
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
|
||||
} catch (Exception exception) {
|
||||
plugin.getLogger().warning(
|
||||
"Could not check for updates: " + exception.getMessage()
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
|
||||
if (!player.isOp()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (latestVersion == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String currentVersion = plugin.getPluginMeta().getVersion();
|
||||
|
||||
if (isNewerVersion(latestVersion, currentVersion)) {
|
||||
player.sendMessage(
|
||||
"§6[MobCage] §eUpdate available! Current: "
|
||||
+ currentVersion
|
||||
+ " | Latest: "
|
||||
+ latestVersion
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNewerVersion(String latest, String current) {
|
||||
try {
|
||||
String[] latestParts = latest.split("\\.");
|
||||
String[] currentParts = current.split("\\.");
|
||||
|
||||
int length = Math.max(latestParts.length, currentParts.length);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int latestNumber = i < latestParts.length
|
||||
? Integer.parseInt(latestParts[i]) : 0;
|
||||
|
||||
int currentNumber = i < currentParts.length
|
||||
? Integer.parseInt(currentParts[i]) : 0;
|
||||
|
||||
if (latestNumber > currentNumber) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (latestNumber < currentNumber) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (NumberFormatException exception) {
|
||||
plugin.getLogger().warning(
|
||||
"Unable to compare plugin versions: " + latest + " and " + current
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user