Skip to content

Commit

Permalink
Minor Code Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Flo56958 committed Jan 1, 2021
1 parent 6b8ea2a commit f16e674
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/flo56958/minetinker/MineTinker.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void onEnable() {
ChatWriter.log(false, LanguageManager.getString("StartUp.StdLogging"));
ChatWriter.log(true, LanguageManager.getString("StartUp.DebugLogging"));

for (Player current : Bukkit.getServer().getOnlinePlayers()) {
for (final Player current : Bukkit.getServer().getOnlinePlayers()) {
Power.HAS_POWER.computeIfAbsent(current, player -> new AtomicBoolean(false));
Lists.BLOCKFACE.put(current, BlockFace.SELF);
}
Expand Down
44 changes: 23 additions & 21 deletions src/main/java/de/flo56958/minetinker/listeners/EntityListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public void onDeath(@NotNull final EntityDeathEvent event) {
Random rand = new Random();
if (rand.nextInt(100) < config.getInt("ConvertMobDrops.Chance", 100)) {
if (!modManager.convertItemStack(item, null)) continue;
//Item is now MT
//continue if already was MT or not right material

if (config.getBoolean("ConvertMobDrops.ApplyExp", true)) {
final int exp = rand.nextInt(config.getInt("ConvertMobDrops.MaximumNumberOfExp", 650));
Expand All @@ -142,7 +144,8 @@ public void onDeath(@NotNull final EntityDeathEvent event) {

if (config.getBoolean("ConvertMobDrops.ApplyModifiers", true)) {
List<Modifier> mods = modManager.getAllowedMods();
for (int i = 0; i < rand.nextInt(config.getInt("ConvertMobDrops.MaximumNumberOfModifiers", 4) + 1); i++) {
for (int i = 0; i < rand.nextInt(
config.getInt("ConvertMobDrops.MaximumNumberOfModifiers", 4) + 1); i++) {
if (config.getBoolean("ConvertMobDrops.AppliedModifiersConsiderSlots", true)
&& modManager.getFreeSlots(item) == 0) {
break;
Expand Down Expand Up @@ -183,8 +186,8 @@ public void onDeath(@NotNull final EntityDeathEvent event) {

Bukkit.getPluginManager().callEvent(new MTEntityDeathEvent(player, tool, event));

modManager.addExp(player, tool, MineTinker.getPlugin().getConfig().getInt("ExtraExpPerEntityDeath."
+ event.getEntity().getType().toString(), 0));
modManager.addExp(player, tool, MineTinker.getPlugin().getConfig()
.getInt("ExtraExpPerEntityDeath." + event.getEntity().getType().toString(), 0));
}

@EventHandler(ignoreCancelled = true)
Expand Down Expand Up @@ -260,7 +263,7 @@ public void onProjectileLaunch(@NotNull final ProjectileLaunchEvent event) {
}

@EventHandler(ignoreCancelled = true)
public void onBowShoot(EntityShootBowEvent event) {
public void onBowShoot(@NotNull final EntityShootBowEvent event) {
if (!(event.getEntity() instanceof Player)) {
return;
}
Expand All @@ -269,31 +272,30 @@ public void onBowShoot(EntityShootBowEvent event) {
final ItemStack offHand = player.getInventory().getItemInOffHand();

if (offHand.getType() == Material.ARROW) {
final Modifier mod = modManager.getModifierFromItem(offHand);

if (mod != null && mod.getModItem().getType() == Material.ARROW) {
event.setCancelled(true);

player.updateInventory();
player.playSound(player.getLocation(), Sound.ITEM_CROSSBOW_LOADING_END, 1.0f, 1.0f);

return;
}
if (playSound(event, player, offHand)) return;
}

for (ItemStack item : player.getInventory().getContents()) {
if (item == null) continue; // Extremely consistently null

if (item.getType() == Material.ARROW) {
final Modifier mod = modManager.getModifierFromItem(item);
if (mod != null && mod.getModItem().getType() == Material.ARROW) {
event.setCancelled(true);
player.updateInventory();
player.playSound(player.getLocation(), Sound.ITEM_CROSSBOW_LOADING_END, 1.0f, 1.0f);
return;
}
if (playSound(event, player, item)) return;
return;
}
}
}

private boolean playSound(final EntityShootBowEvent event, final Player player, final ItemStack offHand) {
final Modifier mod = modManager.getModifierFromItem(offHand);

if (mod != null && mod.getModItem().getType() == Material.ARROW) {
event.setCancelled(true);

player.updateInventory();
player.playSound(player.getLocation(), Sound.ITEM_CROSSBOW_LOADING_END, 1.0f, 1.0f);

return true;
}
return false;
}
}
37 changes: 23 additions & 14 deletions src/main/java/de/flo56958/minetinker/modifiers/ModManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void reload() {
removeRecipes();
mods.clear();

for (Modifier m : allMods) {
for (final Modifier m : allMods) {
m.reload();

if (m.isAllowed()) {
Expand All @@ -280,7 +280,7 @@ public void reload() {
this.ArmorIdentifier = config.getString("ArmorIdentifier");

removeRecipes();
for (Modifier m : this.mods) {
for (final Modifier m : this.mods) {
m.registerCraftingRecipe();
}

Expand Down Expand Up @@ -315,7 +315,7 @@ private void reloadIncompatibilities() {
FileConfiguration modifierconfig = ConfigurationManager.getConfig("Modifiers.yml");

final List<String> possibleKeys = new ArrayList<>();
for (Modifier m : this.allMods) {
for (final Modifier m : this.allMods) {
possibleKeys.add(m.getKey());
}
possibleKeys.sort(String::compareToIgnoreCase);
Expand All @@ -324,17 +324,17 @@ private void reloadIncompatibilities() {
ConfigurationManager.saveConfig(modifierconfig);
ConfigurationManager.loadConfig("", "Modifiers.yml");
incompatibilities.clear();
for (Modifier m : this.allMods) {
for (final Modifier m : this.allMods) {
incompatibilities.putIfAbsent(m, new HashSet<>());
}
modifierconfig = ConfigurationManager.getConfig("Modifiers.yml");
final List<String> incompatibilityList = modifierconfig.getStringList("Incompatibilities");
for (String s : incompatibilityList) {
for (final String s : incompatibilityList) {
final String[] splits = s.split(":");
if (splits.length != 2) continue;
Modifier mod1 = null;
Modifier mod2 = null;
for (Modifier m : this.allMods) {
for (final Modifier m : this.allMods) {
if (m.getKey().equals(splits[0])) {
mod1 = m;
}
Expand All @@ -344,15 +344,24 @@ private void reloadIncompatibilities() {
}

if (mod1 == null || mod2 == null) continue;
if (mod1.equals(mod2)) continue; //Modifier can not be incompatible with its self
if (mod1.equals(mod2)) continue; //Modifier can not be incompatible with itself

//Cross-link incompatibilities
incompatibilities.get(mod1).add(mod2);
incompatibilities.get(mod2).add(mod1);
}

//Make the incompatibilities unmodifiable
incompatibilities.replaceAll((m, set) -> Collections.unmodifiableSet(set));
}

public @NotNull HashSet<Modifier> getIncompatibilities(Modifier m) {
return new HashSet<>(incompatibilities.get(m));
/**
* This Method returns the original Set.
* @param m The modifier to get the Incompatibilities for
* @return The incompatibilities
*/
public @Nullable Set<Modifier> getIncompatibilities(final Modifier m) {
return incompatibilities.get(m);
}

/**
Expand Down Expand Up @@ -426,11 +435,11 @@ public boolean allowBookToModifier() {
*
* @return the modifier list
*/
public List<Modifier> getAllowedMods() {
public @NotNull List<Modifier> getAllowedMods() {
return new ArrayList<>(this.mods);
}

public HashSet<Modifier> getAllMods() {
public @NotNull HashSet<Modifier> getAllMods() {
return new HashSet<>(this.allMods);
}

Expand All @@ -445,15 +454,15 @@ void addMod(@NotNull final ItemStack is, @NotNull final Modifier mod) {
rewriteLore(is);
}

public boolean addMod(final Player player, final ItemStack item, @NotNull final Modifier modifier, final boolean fromCommand, final boolean fromRandom, final boolean silent) {
public boolean addMod(final Player player, @NotNull final ItemStack item, @NotNull final Modifier modifier, final boolean fromCommand, final boolean fromRandom, final boolean silent) {
if (!modifier.getKey().equals(ExtraModifier.instance().getKey())) {
if (!modifier.checkAndAdd(player, item,
modifier.getKey().toLowerCase().replace("-", ""), fromCommand, fromRandom, silent)) {
return false;
}
}

boolean success = modifier.applyMod(player, item, fromCommand);
final boolean success = modifier.applyMod(player, item, fromCommand);

if (success) {
ItemMeta meta = item.getItemMeta();
Expand All @@ -479,7 +488,7 @@ public boolean addMod(final Player player, final ItemStack item, @NotNull final
}

/**
* get the level of a specified modifier on a tool
* get the level of a specified modifier on a tool. 0 on failure
*
* @param is the item
* @param mod the modifier
Expand Down
32 changes: 17 additions & 15 deletions src/main/java/de/flo56958/minetinker/modifiers/Modifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,23 @@ boolean checkAndAdd(Player player, ItemStack tool, String permission, boolean is

if (!(modManager.hasMod(tool, this) && modifiersconfig.getBoolean("IgnoreIncompatibilityIfModifierAlreadyApplied"))) {
if (fromRandom || !(modifiersconfig.getBoolean("CommandIgnoresIncompatibilities") && isCommand)) {
Set<Modifier> incompatibility = modManager.getIncompatibilities(this);

for (Modifier m : incompatibility) {
if (modManager.hasMod(tool, m)) {
if (!silent)
pluginManager.callEvent(new ModifierFailEvent(player, tool, this, ModifierFailCause.INCOMPATIBLE_MODIFIERS, isCommand));
return false;
}
if (modifiersconfig.getBoolean("IncompatibilitiesConsiderEnchants")) {
for (Enchantment e : m.getAppliedEnchantments()) {
if (!tool.hasItemMeta()) return false;
if (Objects.requireNonNull(tool.getItemMeta(), "Tool has no ItemMeta").hasEnchant(e)) {
if (!silent)
pluginManager.callEvent(new ModifierFailEvent(player, tool, this, ModifierFailCause.INCOMPATIBLE_MODIFIERS, isCommand));
return false;
final Set<Modifier> incompatibility = modManager.getIncompatibilities(this);

if (incompatibility != null) {
for (final Modifier m : incompatibility) {
if (modManager.hasMod(tool, m)) {
if (!silent)
pluginManager.callEvent(new ModifierFailEvent(player, tool, this, ModifierFailCause.INCOMPATIBLE_MODIFIERS, isCommand));
return false;
}
if (modifiersconfig.getBoolean("IncompatibilitiesConsiderEnchants")) {
for (final Enchantment e : m.getAppliedEnchantments()) {
if (!tool.hasItemMeta()) return false;
if (Objects.requireNonNull(tool.getItemMeta(), "Tool has no ItemMeta").hasEnchant(e)) {
if (!silent)
pluginManager.callEvent(new ModifierFailEvent(player, tool, this, ModifierFailCause.INCOMPATIBLE_MODIFIERS, isCommand));
return false;
}
}
}
}
Expand Down

0 comments on commit f16e674

Please sign in to comment.