Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update config, adds config migration, adds setPersistance (dont despa… #1

Merged
merged 3 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>me.shansen</groupId>
<groupId>com.minecraftheads</groupId>
<artifactId>EggCatcher</artifactId>
<version>1.2</version>
<version>1.3</version>
<packaging>jar</packaging>

<name>EggCatcher</name>
Expand All @@ -27,21 +27,12 @@
<configuration>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>org.bstats:*</include>
</includes>
<excludes>
<exclude>org.bukkit:bukkit</exclude>
<exclude>org.yaml:snakeyaml</exclude>
<exclude>net.milkbowl.vault:Vault</exclude>
</excludes>
</artifactSet>
<relocations>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>me.shansen.EggCatcher.bstats</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
Expand All @@ -68,10 +59,6 @@
<id>vault-repo</id>
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
</repository>
<repository>
<id>CodeMC</id>
<url>https://repo.codemc.org/repository/maven-public</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -87,10 +74,5 @@
<version>1.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>1.7</version>
</dependency>
</dependencies>
</project>
120 changes: 120 additions & 0 deletions src/main/java/com/minecraftheads/EggCatcher/EggCatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.minecraftheads.EggCatcher;

import com.minecraftheads.EggCatcher.listeners.EggCatcherEntityListener;
import com.minecraftheads.EggCatcher.listeners.EggCatcherPlayerListener;
import com.minecraftheads.EggCatcher.listeners.EggCatcherSpawnListener;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Egg;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

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

public class EggCatcher extends JavaPlugin {
public static List<Egg> eggs = new ArrayList<Egg>();
public static Economy economy = null;

public void onDisable() {
}

public void onEnable() {
this.CheckConfigurationFile();

PluginManager pm = this.getServer().getPluginManager();

final EggCatcherPlayerListener playerListener = new EggCatcherPlayerListener();
final EggCatcherEntityListener entityListener = new EggCatcherEntityListener(this);
final EggCatcherSpawnListener spawnListener = new EggCatcherSpawnListener(this);

pm.registerEvents(playerListener, this);
pm.registerEvents(entityListener, this);
pm.registerEvents(spawnListener, this);

if (getServer().getPluginManager().getPlugin("Vault") != null) {
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration
(Economy.class);
if (economyProvider != null) {
economy = economyProvider.getProvider();
}
}

}

public void CheckConfigurationFile() {
double configVersion = this.getConfig().getDouble("ConfigVersion", 0.0);
if (configVersion > 6.0) {
MigrateConfigFile();
}
if (configVersion == 6.0) {
this.saveConfig();
} else {
this.saveResource("config.yml", true);
this.reloadConfig();
}
}

// This is a ugly piece if shit but i want to make the transition as smooth as possible
public boolean MigrateConfigFile() {
String[] keysWithBoolValue = {"UsePermissions", "UseCatchChance", "LooseEggOnFail", "UseVaultCost", "UseItemCost", "UseHealthPercentage", "ExplosionEffect", "SmokeEffect", "NonPlayerCatching", "PreventCatchingBabyAnimals", "PreventCatchingTamedAnimals", "PreventCatchingShearedSheeps", "SpawnChickenOnSuccess", "SpawnChickenOnFail", "DeleteVillagerInventoryOnCatch", "LogEggCaptures", "setPersistence"};
String[] keysWithStringValue = {"VaultTargetBankAccount"};
String[] entitiesInConfig = {"Axolotl", "Bat", "Bee", "Blaze", "Cat", "CaveSpider", "Chicken", "Cod", "Cow", "Creeper", "Dolphin", "Donkey", "Drowned", "ElderGuardian", "Enderman", "Endermite", "Evoker", "Fox", "Ghast", "GlowSquid", "Goat", "Guardian", "Hoglin", "Horse", "Husk", "Llama", "MagmaCube", "Mule", "MushroomCow", "Ocelot", "Panda", "Parrot", "Phantom", "Pig", "Piglin", "PiglinBrute", "PigZombie", "Pillager", "PolarBear", "Pufferfish", "Rabbit", "Ravager", "Salmon", "Sheep", "Shulker", "Silverfish", "Skeleton", "SkeletonHorse", "Slime", "Spider", "Squid", "Stray", "Strider", "TraderLlama", "TropicalFish", "Turtle", "Vex", "Villager", "Vindicator", "WanderingTrader", "Witch", "WitherSkeleton", "Wolf", "Zoglin", "Zombie", "ZombieHorse", "ZombieVillager", "ZombifiedPiglin"};


FileConfiguration config = this.getConfig();

HashMap <String, HashMap> entityList = new HashMap<>();

for (int i = 0; i < keysWithBoolValue.length; i++) {
config.set(keysWithBoolValue[i], config.getBoolean(keysWithBoolValue[i], true));

}
for (int i = 0; i < keysWithStringValue.length; i++) {
config.set(keysWithStringValue[i], config.getString(keysWithStringValue[i], ""));
}

for (int i = 0; i < entitiesInConfig.length; i++) {
HashMap<String, HashMap> entity = new HashMap<>();
HashMap<String, Double> HealthPercentage = new HashMap<String, Double>();
HashMap<String, Double> CatchChance = new HashMap<>();
HashMap<String, Double> VaultCost = new HashMap<>();
HashMap<String, HashMap> ItemCost = new HashMap<>();
HashMap<String, Double> ItemAmount = new HashMap<>();
HashMap<String, String> ItemName = new HashMap<>();

// generate ItemCost
ItemName.put("ItemName", config.getString("ItemCost.ItemId", "gold_nugget"));
ItemAmount.put("Amount", config.getDouble("ItemCost.Amount." + entitiesInConfig[i], 0.0));
ItemCost.put("ItemCost", ItemAmount);
ItemCost.put("ItemCost", ItemName);

// generate VaultCost
VaultCost.put("VaultCost", config.getDouble("VaultCost." + entitiesInConfig[i], 0.0));

// generate CatchChance
CatchChance.put("CatchChance", config.getDouble("CatchChance." + entitiesInConfig[i], 0.0));

// generate HealthPercentage
HealthPercentage.put("HealthPercentage", config.getDouble("HealthPercentage." + entitiesInConfig[i], 0.0));

// Build new Config Hash for entity
entity.put(entitiesInConfig[i], ItemCost);
entity.put(entitiesInConfig[i], VaultCost);
entity.put(entitiesInConfig[i], HealthPercentage);
entity.put(entitiesInConfig[i], CatchChance);

entityList.put(entitiesInConfig[i], entity);
}
config.set("Entity", entityList);
config.set("Version", 6.0);
config.set("VaultCost", null);
config.set("HealthPercentage", null);
config.set("CatchChance", null);
config.set("ItemCost", null);

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.shansen.EggCatcher;
package com.minecraftheads.EggCatcher;

import java.io.File;
import java.io.FileWriter;
Expand All @@ -7,7 +7,6 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;

//Created 11/11/2016 2:21 AM
public class EggCatcherLogger{
private final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private File file;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
/*
EggCatcher
Copyright (C) 2012, 2013 me@shansen.me

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.shansen.EggCatcher;
package com.minecraftheads.EggCatcher;

import org.bukkit.Material;
import org.bukkit.entity.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
/*
EggCatcher
Copyright (C) 2012, 2013 me@shansen.me, andre@norcode.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.shansen.EggCatcher.events;
package com.minecraftheads.EggCatcher.events;

import org.bukkit.entity.Egg;
import org.bukkit.entity.Entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.shansen.EggCatcher.listeners;
package com.minecraftheads.EggCatcher.listeners;

import me.shansen.EggCatcher.EggCatcher;
import me.shansen.EggCatcher.EggCatcherLogger;
import me.shansen.EggCatcher.EggType;
import me.shansen.EggCatcher.events.EggCaptureEvent;
import com.minecraftheads.EggCatcher.EggCatcherLogger;
import com.minecraftheads.EggCatcher.EggType;
import com.minecraftheads.EggCatcher.EggCatcher;
import com.minecraftheads.EggCatcher.events.EggCaptureEvent;

import org.bukkit.Effect;
import org.bukkit.Material;
Expand All @@ -36,6 +36,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.attribute.Attribute;

import java.io.File;

Expand Down Expand Up @@ -169,9 +170,9 @@ public void onEntityHitByEgg(EntityDamageEvent event) {
}

if (this.useHealthPercentage) {
double healthPercentage = config.getDouble("HealthPercentage." + eggType.getFriendlyName());
double healthPercentage = config.getDouble("Entity." + eggType.getFriendlyName() + ".HealthPercentage");
double currentHealth = ((LivingEntity) entity).getHealth() * 100.0 / ((LivingEntity) entity)
.getMaxHealth();
.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
if (healthPercentage < currentHealth) {
if (this.healthPercentageFailMessage.length() > 0) {
player.sendMessage(String.format(this.healthPercentageFailMessage, healthPercentage));
Expand All @@ -185,7 +186,7 @@ public void onEntityHitByEgg(EntityDamageEvent event) {
}

if (this.useCatchChance) {
double catchChance = config.getDouble("CatchChance." + eggType.getFriendlyName());
double catchChance = config.getDouble("Entity." + eggType.getFriendlyName() + ".CatchChance");
if (Math.random() * 100 <= catchChance) {
if (this.catchChanceSuccessMessage.length() > 0) {
player.sendMessage(catchChanceSuccessMessage);
Expand All @@ -205,16 +206,16 @@ public void onEntityHitByEgg(EntityDamageEvent event) {
boolean freeCatch = player.hasPermission("eggcatcher.free");

if (this.useVaultCost && !freeCatch) {
vaultCost = config.getDouble("VaultCost." + eggType.getFriendlyName());
if (!EggCatcher.economy.has(player.getName(), vaultCost)) {
vaultCost = config.getDouble("Entity." + eggType.getFriendlyName() + ".VaultCost");
if (!EggCatcher.economy.has(player, vaultCost)) {
player.sendMessage(String.format(config.getString("Messages.VaultFail"), vaultCost));
if (!this.looseEggOnFail) {
player.getInventory().addItem(new ItemStack(Material.EGG, 1));
EggCatcher.eggs.add(egg);
}
return;
} else {
EggCatcher.economy.withdrawPlayer(player.getName(), vaultCost);
EggCatcher.economy.withdrawPlayer(player, vaultCost);

if (!this.vaultTargetBankAccount.isEmpty()) {
EggCatcher.economy.bankDeposit(this.vaultTargetBankAccount, vaultCost);
Expand All @@ -225,11 +226,10 @@ public void onEntityHitByEgg(EntityDamageEvent event) {
}

if (this.useItemCost && !freeCatch) {
Material itemMaterial = Material.matchMaterial(config.getString("ItemCost.ItemId", "gold_nugget"));
int itemData = config.getInt("ItemCost.ItemData", 0);
int itemAmount = config.getInt("ItemCost.Amount." + eggType.getFriendlyName(), 0);
@SuppressWarnings("deprecation")
ItemStack itemStack = new ItemStack(itemMaterial, itemAmount, (short) itemData);
Material itemMaterial = Material.matchMaterial(config.getString("Entity." + eggType.getFriendlyName() + ".ItemCost.ItemName", "gold_nugget"));
int itemData = config.getInt("Entity." + eggType.getFriendlyName() + ".ItemCost.ItemData", 0);
int itemAmount = config.getInt("Entity." + eggType.getFriendlyName() + ".ItemCost.Amount", 0);
ItemStack itemStack = new ItemStack(itemMaterial, itemAmount);
if (player.getInventory().containsAtLeast(itemStack, itemStack.getAmount())) {
player.sendMessage(String.format(config.getString("Messages.ItemCostSuccess"),
String.valueOf(itemAmount)));
Expand All @@ -250,7 +250,7 @@ public void onEntityHitByEgg(EntityDamageEvent event) {
return;
}
if (this.useCatchChance) {
double catchChance = config.getDouble("CatchChance." + eggType.getFriendlyName());
double catchChance = config.getDouble("Entity." + eggType.getFriendlyName() + ".CatchChance");
if (Math.random() * 100 > catchChance) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.minecraftheads.EggCatcher.listeners;

import com.minecraftheads.EggCatcher.EggCatcher;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerEggThrowEvent;

public class EggCatcherPlayerListener implements Listener {
@EventHandler
public void onPlayerEggThrow(PlayerEggThrowEvent event) {
if (EggCatcher.eggs.contains(event.getEgg())) {
event.setHatching(false);
EggCatcher.eggs.remove(event.getEgg());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.minecraftheads.EggCatcher.listeners;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;

import org.bukkit.plugin.java.JavaPlugin;

public class EggCatcherSpawnListener implements Listener {

private final boolean setPersistence;

FileConfiguration config;
JavaPlugin plugin;

public EggCatcherSpawnListener(JavaPlugin plugin) {
this.config = plugin.getConfig();
this.plugin = plugin;
this.setPersistence = this.config.getBoolean("setPersistence", true);
}

@EventHandler(ignoreCancelled = false, priority = EventPriority.HIGH)
public void onEntitySpawnedByEgg(CreatureSpawnEvent event) {
if (setPersistence) {
if (!(event instanceof CreatureSpawnEvent)) {
return;
}

if (event.getSpawnReason() == SpawnReason.SPAWNER_EGG) {
event.getEntity().setRemoveWhenFarAway(false);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.shansen.nbt;
package com.minecraftheads.nbt;

import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
Expand Down
Loading