Skip to content

Commit

Permalink
v4.3.52
Browse files Browse the repository at this point in the history
  • Loading branch information
ZombieStriker committed Feb 11, 2019
1 parent 904981a commit 5b15c77
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 94 deletions.
7 changes: 7 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 4.3.52
Revamped spawnedHorse calls. New methods are now used to make the code a bit cleaner.
Fixed CC Error.

Version 4.3.51b
Fixed vulnerability when horses are saved to database.

Version 4.3.51
Edited logging system so it shows the amount of horses loaded, and not just print out all of the names
Fixed perma-death system so you can no longer check stats for horses that have died
Expand Down
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mcMMOHorse
main: com.blueskullgames.horserpg.HorseRPG
version: 4.3.51
version: 4.3.52
description: An awesome role-playing plugin for horses.
authors: [GetGoodKid,Zombie_Striker]
website: http://www.blueskullgames.com
Expand Down
163 changes: 99 additions & 64 deletions src/com/blueskullgames/horserpg/HorseRPG.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class HorseRPG extends JavaPlugin {
public static BukkitTask saveTask, cooldownTask;
public static ConfigAccessor config;
public static Economy econ;
public static HashMap<Entity, RPGHorse> hSpawnedHorses;
public static HashMap<Entity, RPGHorse> hSpawnedHorsesHashmap;
public static HashMap<UUID, RPGHorse> pCurrentHorse;
public static HashMap<UUID, RPGHorse> offers;
public static HashMap<String, TreeSet<RPGHorse>> ownedHorses;
Expand Down Expand Up @@ -232,7 +232,7 @@ public static RPGHorse getHorseNotSpawned(Player p, String[] args, int offset) {
if (args.length <= offset) {
if (ownedHorses.containsKey(p.getName()) && ownedHorses.get(p.getName()).size() > 0) {
for (RPGHorse h : ownedHorses.get(p.getName())) {
if (!hSpawnedHorses.containsKey(h.getHorse())) {
if (!isRPGHorse(h.getHorse())) {
return h;
}
}
Expand Down Expand Up @@ -266,7 +266,7 @@ public static RPGHorse getHorseSpawned(Player p, String[] args, int offset) {
if (args.length <= offset) {
if (ownedHorses.containsKey(p.getName()) && ownedHorses.get(p.getName()).size() > 0) {
for (RPGHorse h : ownedHorses.get(p.getName())) {
if (hSpawnedHorses.containsKey(h.getHorse())) {
if (isRPGHorse(h.getHorse())) {
return h;
}
}
Expand All @@ -281,7 +281,7 @@ public static RPGHorse getHorseSpawned(Player p, String[] args, int offset) {

if (ownedHorses.containsKey(p.getName()))
for (RPGHorse h : ownedHorses.get(p.getName()))
if (hSpawnedHorses.containsKey(h.getHorse()))
if (isRPGHorse(h.getHorse()))
if (h.name.equalsIgnoreCase(hName))
return h;
msg(p, HORSE_DOES_NOT_EXIST.replace("%name%", hName));
Expand Down Expand Up @@ -633,7 +633,7 @@ public static void claimHorse(CommandSender sender) {
if (rpg.getHorse() != null && !rpg.getHorse().isValid()) {
if (horse.getUniqueId().equals(rpg.getHorse().getUniqueId())
|| horse.getUniqueId().equals(rpg.holderOverUUID)) {
HorseRPG.hSpawnedHorses.put(horse, HorseRPG.hSpawnedHorses.remove(rpg.getHorse()));
updateHorseInstance(horse, rpg.getHorse(), rpg);
rpg.getHorse().remove();
rpg.setHorse(horse);
break;
Expand All @@ -645,8 +645,8 @@ public static void claimHorse(CommandSender sender) {
}
}

if (hSpawnedHorses.containsKey(horse)) {
if (hSpawnedHorses.get(horse).owners_name.equals(sender.getName())) {
if (isRPGHorse(horse)) {
if (getHorse(horse).owners_name.equals(sender.getName())) {
msg(p, "&aYou already own this horse!");
} else {
msg(p, "&aThis horse is already owned!");
Expand Down Expand Up @@ -717,7 +717,7 @@ public static void claimHorse(CommandSender sender) {
h.setName(h.name);

pCurrentHorse.put(p.getUniqueId(), h);
hSpawnedHorses.put(horse, h);
addSpawnedHorse(horse, h);
if (!ownedHorses.containsKey(p.getName()))
ownedHorses.put(p.getName(), new TreeSet<RPGHorse>());
ownedHorses.get(p.getName()).add(h);
Expand Down Expand Up @@ -893,7 +893,7 @@ public static void summonHorse(CommandSender sender, String[] args) {
h = getHorseNotSpawned(p, args, 1);
}
if (h != null) {
if (hSpawnedHorses.containsKey(h.getHorse())) {
if (isRPGHorse(h.getHorse())) {
msg(p, "&e" + h.name + "&a has already been summoned.");
return;
}
Expand Down Expand Up @@ -970,7 +970,7 @@ public static void banishHorse(CommandSender sender, String[] args, boolean forc
if (rpg.getHorse() != null && !rpg.getHorse().isValid()) {
for (Entity e : rpg.getHorse().getNearbyEntities(60, 30, 60)) {
if (e.getUniqueId().equals(rpg.getHorse().getUniqueId())) {
HorseRPG.hSpawnedHorses.put(e, HorseRPG.hSpawnedHorses.remove(rpg.getHorse()));
updateHorseInstance(e, rpg.getHorse(), rpg);
rpg.getHorse().remove();
rpg.setHorse(e);
break;
Expand Down Expand Up @@ -1020,7 +1020,7 @@ public static void banishHorse(CommandSender sender, String[] args, boolean forc
if (pCurrentHorse.get(p.getUniqueId()) == horse) {
pCurrentHorse.remove(p.getUniqueId());
for (RPGHorse others : ownedHorses.get(p.getName())) {
if (hSpawnedHorses.containsKey(others.getHorse())) {
if (isRPGHorse(others.getHorse())) {
pCurrentHorse.put(p.getUniqueId(), others);
break;
}
Expand Down Expand Up @@ -1474,9 +1474,9 @@ public static void deleteHorse(CommandSender sender, String[] args) {
horses.remove(h);
// Test if this removes horses.
h_config.removeHorse(h);
if (hSpawnedHorses.containsKey(h.getHorse())) {
if (isRPGHorse(h.getHorse())) {
h.getHorse().remove();
hSpawnedHorses.remove(h.getHorse());
removeHorseInstance(h.getHorse());
}

msg(p, "&b" + h.name + "&a won't be bothering you anymore.");
Expand All @@ -1492,12 +1492,12 @@ public void loadDatabase(CommandSender sender) {
if (notAllowed(sender, H_DB, false))
return;

for (RPGHorse h : hSpawnedHorses.values())
for (RPGHorse h : getRPGHorseInstances())
h.banish();

ownedHorses.clear();
pCurrentHorse.clear();
hSpawnedHorses.clear();
hSpawnedHorsesHashmap.clear();
horses.clear();

initHorses();
Expand Down Expand Up @@ -1546,7 +1546,7 @@ public void onEnable() {
new BukkitRunnable() {
@Override
public void run() {
for (RPGHorse h : hSpawnedHorses.values()) {
for (RPGHorse h : getRPGHorseInstances()) {
h_config.saveHorse(h, false);
}
h_config.save();
Expand Down Expand Up @@ -1634,7 +1634,7 @@ private void initVariables() {
offers = new HashMap<UUID, RPGHorse>();
ownedHorses = new HashMap<String, TreeSet<RPGHorse>>();
pCurrentHorse = new HashMap<UUID, RPGHorse>();
hSpawnedHorses = new HashMap<Entity, RPGHorse>();
hSpawnedHorsesHashmap = new HashMap<Entity, RPGHorse>();
horses = new HashSet<RPGHorse>();
}

Expand Down Expand Up @@ -1938,53 +1938,60 @@ public static void saveHorses(CommandSender sender) {
}
}
h_config.save();
} else {
try (Connection connect = DriverManager.getConnection("jdbc:sqlite:horses.db")) {
try (Statement statement = connect.createStatement()) {
statement.setQueryTimeout(30);
statement.executeUpdate("drop table if exists horses");
}

try (Statement statement = connect.createStatement()) {
statement.setQueryTimeout(30);
statement.executeUpdate("create table horses ( name string, " + "owner string, " + "color string, "
+ "style string, " + "variant string, " + "godmode integer, " + "swiftnessXP integer, "
+ "agilityXP integer, " + "vitalityXP integer, "
+ "wrathXP integer, sex integer, defaultSpeed integer, defaultJump integer)");
} else {try (Connection connect = DriverManager.getConnection("jdbc:sqlite:horses.db")) {
try (Statement statement = connect.createStatement()) {
statement.setQueryTimeout(30);
statement.executeUpdate("drop table if exists horses");
}
try (Statement statement = connect.createStatement()) {
statement.setQueryTimeout(30);
statement.executeUpdate("create table horses ( name string, " + "owner string, " + "color string, "
+ "style string, " + "variant string, " + "godmode integer, " + "swiftnessXP integer, "
+ "agilityXP integer, " + "vitalityXP integer, "
+ "wrathXP integer, sex integer, defaultSpeed integer, defaultJump integer)");
}
try (PreparedStatement statement = connect.prepareStatement("insert into horses values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")) {
statement.setQueryTimeout(30);
for (TreeSet<RPGHorse> horseSet : ownedHorses.values()) {
for (RPGHorse h : horseSet)
try {
statement.setString(1, h.name);
statement.setString(2, h.owners_name);
statement.setString(3, h.color.toString());
statement.setString(4, h.style.toString());
statement.setString(5, h.variant.toString());
statement.setInt(6, h.godmode ? 1 : 0);
statement.setInt(7, h.swiftness.xp);
statement.setInt(8, h.agility.xp);
statement.setInt(9, h.vitality.xp);
statement.setInt(10, h.wrath.xp);
statement.setInt(11, h.isMale ? 0 : 1);
statement.setInt(12, (int) h.generic_speed);
statement.setInt(13, (int) h.generic_jump);
statement.addBatch();
} catch (Error | Exception ed4) {
ed4.printStackTrace();
}
}
statement.executeBatch();
}

try (PreparedStatement statement = connect.prepareStatement("insert into horses values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")) {
statement.setQueryTimeout(30);
for (TreeSet<RPGHorse> horseSet : ownedHorses.values()) {
for (RPGHorse h : horseSet)
try {
statement.setString(1, h.name);
statement.setString(2, h.owners_name);
statement.setString(3, h.color.toString());
statement.setString(4, h.style.toString());
statement.setString(5, h.variant.toString());
statement.setInt(6, h.godmode ? 1 : 0);
statement.setInt(7, h.swiftness.xp);
statement.setInt(8, h.agility.xp);
statement.setInt(9, h.vitality.xp);
statement.setInt(10, h.wrath.xp);
statement.setInt(11, h.isMale ? 0 : 1);
statement.setInt(12, (int) h.generic_speed);
statement.setInt(13, (int) h.generic_jump);
statement.addBatch();
} catch (Error | Exception ed4) {
ed4.printStackTrace();
}
if (!connect.getAutoCommit()) {
connect.commit();
}/*
for (TreeSet<RPGHorse> horseSet : ownedHorses.values()) {
for (RPGHorse h : horseSet)
try {
statement.executeUpdate("insert into horses values('" + h.name + "', '" + h.owners_name
+ "', '" + h.color + "', '" + h.style + "', '" + h.variant + "', "
+ (h.godmode ? 1 : 0) + ", " + h.swiftness.xp + ", " + h.agility.xp + ", "
+ h.vitality.xp + ", " + h.wrath.xp + ", " + (h.isMale ? 0 : 1) + ", "
+ h.generic_speed + ", " + h.generic_jump + ")");
} catch (Error | Exception ed4) {
ed4.printStackTrace();
}

statement.executeBatch();
}

if (!connect.getAutoCommit()) {
connect.commit();
}
}
}
}*/
}}

if (sender != null)
msg(sender, HORSES_SAVED);
Expand Down Expand Up @@ -2091,7 +2098,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}
if (sender instanceof Player) {
Player player = (Player) sender;
if (player.getVehicle() != null && hSpawnedHorses.containsKey(player.getVehicle())) {
if (player.getVehicle() != null && isRPGHorse(player.getVehicle())) {

}
}
Expand Down Expand Up @@ -2220,15 +2227,15 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
@Override
public void onDisable() {
if (banishondisable)
for (Entry<Entity, RPGHorse> horse : hSpawnedHorses.entrySet()) {
for (Entry<Entity, RPGHorse> horse : getRPGHorseEntrys()) {
horse.getValue().banish();
if (horse.getKey() != null && horse.getKey().isValid()) {
horse.getKey().remove();
}
}

// Force save all spawned horses.
for (RPGHorse h : hSpawnedHorses.values())
for (RPGHorse h : getRPGHorseInstances())
h_config.saveHorse(h, false);
h_config.save();

Expand Down Expand Up @@ -2301,4 +2308,32 @@ public static void showLeaderBoard(CommandSender sender, String[] args) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(instance, new ScoreboardTask(p, oldsb), 200);
}
}





public static void addSpawnedHorse(Entity horse, RPGHorse instance) {
hSpawnedHorsesHashmap.put(horse, instance);
}
public static RPGHorse getHorse(Entity horse) {
return hSpawnedHorsesHashmap.get(horse);
}
public static RPGHorse removeHorseInstance(Entity horse) {
return hSpawnedHorsesHashmap.remove(horse);
}
public static void updateHorseInstance(Entity newEntity, Entity holdinstance, RPGHorse horse) {
hSpawnedHorsesHashmap.remove(holdinstance);
hSpawnedHorsesHashmap.put(newEntity, horse);
}
public static Collection<RPGHorse> getRPGHorseInstances(){
return new ArrayList<>(hSpawnedHorsesHashmap.values());
}
public static Set<Entry<Entity, RPGHorse>> getRPGHorseEntrys(){
return hSpawnedHorsesHashmap.entrySet();
}
public static boolean isRPGHorse(Entity horse) {
return hSpawnedHorsesHashmap.containsKey(horse);
}

}
20 changes: 10 additions & 10 deletions src/com/blueskullgames/horserpg/RPGHorse.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class RPGHorse implements Comparable<RPGHorse> {
public static double maxSpeed = 0.33;
public static double minJump = 0.4;
public static double maxJump = 1.0;

public boolean spawned = false;

public static double getRandomSpeed() {
Expand Down Expand Up @@ -340,15 +340,15 @@ public Entity getHorse() {
for (Entity e : horse.getWorld().getEntities()) {
if (e.getUniqueId().equals(horse.getUniqueId()) || e.getUniqueId().equals(holderOverUUID)) {
holderOverUUID = e.getUniqueId();
HorseRPG.hSpawnedHorses.put(e, this);
HorseRPG.addSpawnedHorse(e, this);
return horse = e;
}
}
return horse;
}

public void setHorse(Entity horse) {
HorseRPG.hSpawnedHorses.put(horse, HorseRPG.hSpawnedHorses.remove(this.horse));
HorseRPG.updateHorseInstance(horse, this.horse, this);
this.horse = horse;
this.holderOverUUID = horse.getUniqueId();
spawned = true;
Expand Down Expand Up @@ -383,7 +383,7 @@ public void setVariant(Variant newVariant) {
this.holderOverUUID = horse.getUniqueId();
spawned = true;
temp.remove();
HorseRPG.hSpawnedHorses.put(this.horse, HorseRPG.hSpawnedHorses.remove(temp));
HorseRPG.updateHorseInstance(this.horse, temp, this);
}
}

Expand Down Expand Up @@ -474,10 +474,10 @@ public Entity summon(Player p, Location loc) {
((Ageable) horse).setAgeLock(false);
((Ageable) horse).setAge(babyAge);
}

holderOverUUID = horse.getUniqueId();
HorseRPG.hSpawnedHorses.put(horse, HorseRPG.hSpawnedHorses.remove(this.horse));
HorseRPG.updateHorseInstance(horse, this.horse, this);

((Tameable) horse).setTamed(true);
horse.setCustomName(ChatColor.translateAlternateColorCodes('&', name));
if (inventory == null) {
Expand Down Expand Up @@ -532,7 +532,7 @@ public Entity summon(Player p, Location loc) {
((Damageable) horse).setHealth(((Damageable) horse).getMaxHealth());
}

HorseRPG.hSpawnedHorses.put(horse, this);
HorseRPG.addSpawnedHorse(horse, this);

this.vitality.update();
this.agility.update();
Expand Down Expand Up @@ -566,7 +566,7 @@ public void banish(boolean timer) {
&& (((Horse) horse).getInventory().getItem(0).getType() == Material.SADDLE));
inventory = ((Horse) horse).getInventory().getContents();
}
HorseRPG.hSpawnedHorses.remove(horse);
HorseRPG.removeHorseInstance(horse);
try {
if (horse instanceof org.bukkit.entity.Donkey) {
setHasChest(((org.bukkit.entity.Donkey) horse).isCarryingChest());
Expand All @@ -581,7 +581,7 @@ public void banish(boolean timer) {
babyAge = ((Ageable) horse).getAge();
horse.eject();
horse.remove();
HorseRPG.hSpawnedHorses.remove(horse, this);
HorseRPG.removeHorseInstance(horse);
horse = null;
holderOverUUID = null;
}
Expand Down
Loading

0 comments on commit 5b15c77

Please sign in to comment.