Skip to content

Commit

Permalink
Add more player-configurable Options
Browse files Browse the repository at this point in the history
  • Loading branch information
Flo56958 committed Dec 11, 2024
1 parent 2b4e2f0 commit 274d681
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 24 deletions.
20 changes: 16 additions & 4 deletions src/main/java/de/flo56958/minetinker/modifiers/types/Drilling.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import de.flo56958.minetinker.api.events.MTBlockBreakEvent;
import de.flo56958.minetinker.api.events.MTPlayerInteractEvent;
import de.flo56958.minetinker.data.ToolType;
import de.flo56958.minetinker.modifiers.Modifier;
import de.flo56958.minetinker.modifiers.PlayerConfigurableModifier;
import de.flo56958.minetinker.utils.ChatWriter;
import de.flo56958.minetinker.utils.ConfigurationManager;
import de.flo56958.minetinker.utils.data.DataHandler;
import de.flo56958.minetinker.utils.playerconfig.PlayerConfigurationManager;
import de.flo56958.minetinker.utils.playerconfig.PlayerConfigurationOption;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
Expand All @@ -28,7 +30,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Drilling extends Modifier implements Listener {
public class Drilling extends PlayerConfigurableModifier implements Listener {

public static final ConcurrentHashMap<Location, Integer> events_break = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<Location, Integer> events_interact = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -120,6 +122,9 @@ public void reload() {

final List<String> blacklistConfig = config.getStringList("Blacklist");
blacklist.addAll(blacklistConfig.stream().map(Material::getMaterial).toList());

CLAMP_LEVEL = new PlayerConfigurationOption(this, "clamp-to-level", PlayerConfigurationOption.Type.INTEGER,
"clamp-to-level", this.getMaxLvl());
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
Expand Down Expand Up @@ -155,7 +160,7 @@ public void effect(@NotNull final MTPlayerInteractEvent event) {

final boolean power = modManager.hasMod(tool, Power.instance());

final int level = modManager.getModLevel(tool, this);
final int level = Math.min(modManager.getModLevel(tool, this), PlayerConfigurationManager.getInstance().getInteger(player, CLAMP_LEVEL));
final BlockFace finalFace = face.getOppositeFace();
for (int i = 1; i <= level; i++) {
final Block b = block.getRelative(finalFace, i);
Expand All @@ -182,7 +187,7 @@ public void effect(@NotNull final MTBlockBreakEvent event) {
if (block.isLiquid()) return; // does not work with liquids
if (!canUseDrilling(player, tool, block.getLocation())) return;

final int level = modManager.getModLevel(tool, this);
final int level = Math.min(modManager.getModLevel(tool, this), PlayerConfigurationManager.getInstance().getInteger(player, CLAMP_LEVEL));

// Check if power save the old correct blockface
BlockFace face = Power.drillingCommunication.remove(block.getLocation());
Expand Down Expand Up @@ -222,4 +227,11 @@ private boolean drillingBlockBreak(final Block block, final float centralBlockHa
return false;
}
}

private PlayerConfigurationOption CLAMP_LEVEL;

@Override
public List<PlayerConfigurationOption> getPCIOptions() {
return List.of(CLAMP_LEVEL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import de.flo56958.minetinker.MineTinker;
import de.flo56958.minetinker.data.ToolType;
import de.flo56958.minetinker.modifiers.Modifier;
import de.flo56958.minetinker.modifiers.PlayerConfigurableModifier;
import de.flo56958.minetinker.utils.ChatWriter;
import de.flo56958.minetinker.utils.ConfigurationManager;
import de.flo56958.minetinker.utils.LanguageManager;
import de.flo56958.minetinker.utils.data.DataHandler;
import de.flo56958.minetinker.utils.playerconfig.PlayerConfigurationManager;
import de.flo56958.minetinker.utils.playerconfig.PlayerConfigurationOption;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
Expand All @@ -21,7 +23,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Photosynthesis extends Modifier {
public class Photosynthesis extends PlayerConfigurableModifier {

private static Photosynthesis instance;
private final ConcurrentHashMap<Player, Tupel> data = new ConcurrentHashMap<>();
Expand All @@ -33,7 +35,6 @@ public class Photosynthesis extends Modifier {
private boolean fullEffectAtNoon;
private boolean allowOffhand;
private boolean mustStandStill;
private boolean notifyWhenActive;

private Photosynthesis() {
super(MineTinker.getPlugin());
Expand Down Expand Up @@ -86,7 +87,6 @@ public void reload() {
config.addDefault("FullEffectAtNoon", true); //if false: full effect always in daylight
config.addDefault("AllowOffHand", true); //if false: only main hand
config.addDefault("MustStandStill", false); //if true: Players need to stand still
config.addDefault("NotifyWhenActive", false); //Notifies the Player via Actionbar

config.addDefault("EnchantCost", 10);
config.addDefault("Enchantable", false);
Expand Down Expand Up @@ -114,7 +114,6 @@ public void reload() {
this.fullEffectAtNoon = config.getBoolean("FullEffectAtNoon", true);
this.allowOffhand = config.getBoolean("AllowOffHand", true);
this.mustStandStill = config.getBoolean("MustStandStill", false);
this.notifyWhenActive = config.getBoolean("NotifyWhenActive", false);

this.description = this.description
.replace("%amount", String.valueOf(healthRepair))
Expand Down Expand Up @@ -218,7 +217,7 @@ public void reload() {
triggered = true;
}

if (triggered && notifyWhenActive)
if (triggered && PlayerConfigurationManager.getInstance().getBoolean(player, NOTIFY_WHEN_ACTIVE))
ChatWriter.sendActionBar(player, this.getColor()
+ LanguageManager.getString("Modifier.Photosynthesis.NotifyWhenActive", player));
}
Expand All @@ -227,6 +226,15 @@ public void reload() {
this.taskID = -1;
}

private PlayerConfigurationOption NOTIFY_WHEN_ACTIVE =
new PlayerConfigurationOption(this, "notify-when-active", PlayerConfigurationOption.Type.BOOLEAN,
"notify-when-active", false);

@Override
public List<PlayerConfigurationOption> getPCIOptions() {
return List.of(NOTIFY_WHEN_ACTIVE);
}

//------------------------------------------------------

private static class Tupel {
Expand Down
37 changes: 26 additions & 11 deletions src/main/java/de/flo56958/minetinker/modifiers/types/Power.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import de.flo56958.minetinker.api.events.MTBlockBreakEvent;
import de.flo56958.minetinker.api.events.MTPlayerInteractEvent;
import de.flo56958.minetinker.data.ToolType;
import de.flo56958.minetinker.modifiers.Modifier;
import de.flo56958.minetinker.modifiers.PlayerConfigurableModifier;
import de.flo56958.minetinker.utils.ChatWriter;
import de.flo56958.minetinker.utils.ConfigurationManager;
import de.flo56958.minetinker.utils.PlayerInfo;
import de.flo56958.minetinker.utils.data.DataHandler;
import de.flo56958.minetinker.utils.playerconfig.PlayerConfigurationManager;
import de.flo56958.minetinker.utils.playerconfig.PlayerConfigurationOption;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -29,7 +31,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Power extends Modifier implements Listener {
public class Power extends PlayerConfigurableModifier implements Listener {

public static final ConcurrentHashMap<Location, Integer> events_break = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<Location, Integer> events_interact = new ConcurrentHashMap<>();
Expand All @@ -39,7 +41,6 @@ public class Power extends Modifier implements Listener {
private static Power instance;
private HashSet<Material> blacklist = new HashSet<>();
private boolean treatAsWhitelist;
private boolean lv1_vertical;
private boolean toggleable;

private Power() {
Expand Down Expand Up @@ -73,7 +74,6 @@ public void reload() {

config.addDefault("Allowed", true);
config.addDefault("Color", "%GREEN%");
config.addDefault("Lv1Vertical", false); // Should the 3x1 at level 1 be horizontal (false) or vertical (true)
config.addDefault("Toggleable", true);
config.addDefault("MaxLevel", 2); // Algorithm for area of effect (except for level 1): (level * 2) - 1 x
config.addDefault("SlotCost", 2);
Expand Down Expand Up @@ -108,14 +108,16 @@ public void reload() {

init();

this.lv1_vertical = config.getBoolean("Lv1Vertical", false);
this.toggleable = config.getBoolean("Toggleable", true);
this.treatAsWhitelist = config.getBoolean("TreatAsWhitelist", false);

blacklist.clear();

final List<String> blacklistConfig = config.getStringList("Blacklist");
blacklist.addAll(blacklistConfig.stream().map(Material::getMaterial).toList());

CLAMP_LEVEL = new PlayerConfigurationOption(this, "clamp-to-level", PlayerConfigurationOption.Type.INTEGER,
"clamp-to-level", this.getMaxLvl());
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
Expand All @@ -126,16 +128,16 @@ public boolean canUsePower(final Player player, final ItemStack tool) {
return modManager.hasMod(tool, this);
}

private Set<Block> getPowerBlocks(@NotNull ItemStack tool, @NotNull Block block, @NotNull BlockFace face, @NotNull PlayerInfo.Direction direction) {
final int level = modManager.getModLevel(tool, this);
private Set<Block> getPowerBlocks(@NotNull final Player player, @NotNull final ItemStack tool, @NotNull final Block block, @NotNull final BlockFace face, @NotNull final PlayerInfo.Direction direction) {
final int level = Math.min(modManager.getModLevel(tool, this), PlayerConfigurationManager.getInstance().getInteger(player, CLAMP_LEVEL));

final HashSet<Block> blocks = new HashSet<>();

final boolean down_up = face.equals(BlockFace.DOWN) || face.equals(BlockFace.UP);
final boolean north_south = face.equals(BlockFace.NORTH) || face.equals(BlockFace.SOUTH);
if (level == 1) {
Block b1 = null, b2 = null;
if (lv1_vertical) {
if (PlayerConfigurationManager.getInstance().getBoolean(player, LEVEL_1_VERTICAL)) {
if (down_up) {
if (direction == PlayerInfo.Direction.NORTH || direction == PlayerInfo.Direction.SOUTH) {
b1 = block.getWorld().getBlockAt(block.getLocation().add(0, 0, 1));
Expand Down Expand Up @@ -165,7 +167,7 @@ private Set<Block> getPowerBlocks(@NotNull ItemStack tool, @NotNull Block block,
}
blocks.add(b1);
blocks.add(b2);
} else {
} else if (level > 1) {
if (down_up) {
for (int x = -(level - 1); x <= (level - 1); x++) {
for (int z = -(level - 1); z <= (level - 1); z++) {
Expand Down Expand Up @@ -231,7 +233,7 @@ public void effect(@NotNull final MTBlockBreakEvent event) {

final boolean drilling = modManager.hasMod(tool, Drilling.instance());

for (final Block b : getPowerBlocks(tool, block, face, direction)) {
for (final Block b : getPowerBlocks(player, tool, block, face, direction)) {
if (b.getType().isAir()) continue;
if (b.isLiquid()) continue;

Expand All @@ -258,7 +260,7 @@ public void onInteract(@NotNull final MTPlayerInteractEvent event) {
final BlockFace face = event.getEvent().getBlockFace();

// Broadcast InteractEvent to other Blocks
final Set<Block> blocks = getPowerBlocks(tool, block, face, PlayerInfo.getFacingDirection(player));
final Set<Block> blocks = getPowerBlocks(player, tool, block, face, PlayerInfo.getFacingDirection(player));
blocks.remove(block); // Remove the central block (already handled in the BlockBreakEvent)
for (final Block b : blocks) {
if (b.getType().isAir()) continue;
Expand Down Expand Up @@ -379,4 +381,17 @@ private void powerBlockBreak(@Nullable final Block block, final float centralBlo
DataHandler.playerBreakBlock(player, block, tool);
} catch (IllegalArgumentException ignored) {}
}

private PlayerConfigurationOption LEVEL_1_VERTICAL =
new PlayerConfigurationOption(this, "level-1-vertical", PlayerConfigurationOption.Type.BOOLEAN,
"level-1-vertical", false);

private PlayerConfigurationOption CLAMP_LEVEL;

@Override
public List<PlayerConfigurationOption> getPCIOptions() {
final ArrayList<PlayerConfigurationOption> playerConfigurationOptions = new ArrayList<>(List.of(LEVEL_1_VERTICAL, CLAMP_LEVEL));
playerConfigurationOptions.sort(Comparator.comparing(PlayerConfigurationOption::displayName));
return playerConfigurationOptions;
}
}
17 changes: 14 additions & 3 deletions src/main/java/de/flo56958/minetinker/modifiers/types/Timber.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import de.flo56958.minetinker.MineTinker;
import de.flo56958.minetinker.api.events.MTBlockBreakEvent;
import de.flo56958.minetinker.data.ToolType;
import de.flo56958.minetinker.modifiers.Modifier;
import de.flo56958.minetinker.modifiers.PlayerConfigurableModifier;
import de.flo56958.minetinker.utils.ChatWriter;
import de.flo56958.minetinker.utils.ConfigurationManager;
import de.flo56958.minetinker.utils.LanguageManager;
import de.flo56958.minetinker.utils.data.DataHandler;
import de.flo56958.minetinker.utils.playerconfig.PlayerConfigurationManager;
import de.flo56958.minetinker.utils.playerconfig.PlayerConfigurationOption;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -27,7 +29,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Timber extends Modifier implements Listener {
public class Timber extends PlayerConfigurableModifier implements Listener {

private static Timber instance;
private int maxBlocks;
Expand Down Expand Up @@ -158,7 +160,7 @@ public void effect(@NotNull MTBlockBreakEvent event) {
});

// Place sapling on all ground blocks if applicable
if (saplingType == null || level < 2 || groundBlocks.isEmpty()) return;
if (saplingType == null || level < 2 || groundBlocks.isEmpty() || !PlayerConfigurationManager.getInstance().getBoolean(player, PLANT_SAMPLINGS)) return;
// sort ground blocks by distance to the original block (closest first)
groundBlocks.sort(Comparator.comparing(b -> {
final Location l = b.getLocation();
Expand Down Expand Up @@ -237,4 +239,13 @@ public List<String> getStatistics(ItemStack item) {
.replaceAll("%amount", String.valueOf(stat)));
return lore;
}

private final PlayerConfigurationOption PLANT_SAMPLINGS =
new PlayerConfigurationOption(this, "plant-samplings", PlayerConfigurationOption.Type.BOOLEAN,
"plant-saplings", true);

@Override
public List<PlayerConfigurationOption> getPCIOptions() {
return List.of(PLANT_SAMPLINGS);
}
}

0 comments on commit 274d681

Please sign in to comment.