Skip to content

Commit

Permalink
add pullStrength and pickUpCheck options
Browse files Browse the repository at this point in the history
- modes.Range.pullStrengthMultiplier
- modes.Range.skipCanPickUpCheck
  • Loading branch information
arnokeesman committed Feb 27, 2024
1 parent 7347add commit 25d7b23
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ This mod adds a magnet command to the game which allows you to pull items toward
### Config
The config is located at `/config/magnet-command.json`.

| Option | Description | Default value |
|---------------------------------------|----------------------------------------------------------------------------|---------------|
| `permissionLevel` | The default permission/OP level for all modes | 2 |
| `modes.*.enabled` | Enable/disable modes | true |
| `modes.Range.range` | The range in blocks for Range mode | 3 |
| `modes.Range.moveMode` | Movement mode for the magnet. Options: Pull, Teleport | "Pull" |
| `modes.OnBreak.dropLocation` | Where to drop items if they don't fit in inventory. Options: Block, Player | "Block" |
| `modes.OnBreak.includeContainerItems` | Enable/disable injecting items from containers like chests and furnaces | true |
| Option | Description | Default value |
|---------------------------------------|-------------------------------------------------------------------------------|---------------|
| `permissionLevel` | The default permission/OP level for all modes | 2 |
| `modes.*.enabled` | Enable/disable modes | true |
| `modes.Range.range` | The range in blocks for Range mode | 3 |
| `modes.Range.mode` | Movement mode for the magnet. Options: Pull, Teleport | "Pull" |
| `modes.Range.pullStrengthMultiplier` | Increase or decrease the pull speed for items | 1.0 |
| `modes.Range.skipCanPickUpCheck` | Skipping this check makes it so that items are pulled towards you immediately | false |
| `modes.OnBreak.dropLocation` | Where to drop items if they don't fit in inventory. Options: Block, Player | "Block" |
| `modes.OnBreak.includeContainerItems` | Enable/disable injecting items from containers like chests and furnaces | true |
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.14.21

# Mod Properties
mod_version=0.2.2
mod_version=0.2.3
maven_group=dev.keesmand
archives_base_name=magnet-command
supported_minecraft_version=1.20.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ public class MagnetCommandConfig {
public final boolean onBreakEnabled;
public final DropMode dropLocation;
public final boolean includeContainerItems;
public final double pullStrengthMultiplier;
public final boolean skipCanPickUpCheck;

public MagnetCommandConfig(int permissionLevel, boolean rangeEnabled, int range, MoveMode moveMode, boolean onBreakEnabled, DropMode dropLocation, boolean includeContainerItems) {
public MagnetCommandConfig(int permissionLevel, boolean rangeEnabled, int range, MoveMode moveMode, boolean onBreakEnabled, DropMode dropLocation, boolean includeContainerItems, double pullStrengthMultiplier, boolean skipCanPickUpCheck) {
this.permissionLevel = permissionLevel;
this.rangeEnabled = rangeEnabled;
this.range = range;
this.moveMode = moveMode;
this.onBreakEnabled = onBreakEnabled;
this.dropLocation = dropLocation;
this.includeContainerItems = includeContainerItems;
this.pullStrengthMultiplier = pullStrengthMultiplier;
this.skipCanPickUpCheck = skipCanPickUpCheck;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static void save(MagnetCommandConfig config) {
rangeMode.addProperty("range", config.range);
rangeMode.addProperty("_c1", "moveMode can be either Pull or Teleport");
rangeMode.addProperty("moveMode", config.moveMode.name());
rangeMode.addProperty("pullStrengthMultiplier", config.pullStrengthMultiplier);
rangeMode.addProperty("skipCanPickUpCheck", config.skipCanPickUpCheck);

JsonObject onBreakMode = new JsonObject();
modes.add("OnBreak", onBreakMode);
Expand Down Expand Up @@ -60,6 +62,8 @@ public static MagnetCommandConfig load() {
}

try {
MagnetCommandConfig defaultConfig = null;

JsonObject jsonConfig = new Gson().fromJson(Files.readString(Path.of(configFile)), JsonObject.class);
int configVersion = jsonConfig.get("CONFIG_VERSION").getAsInt();
if (configVersion != 1) throw new IllegalArgumentException("unknown config version");
Expand All @@ -76,16 +80,47 @@ public static MagnetCommandConfig load() {
if (!isValidEnumValue(MoveMode.class, moveModeString))
throw new IllegalArgumentException("unknown moveMode");
MoveMode moveMode = MoveMode.valueOf(moveModeString);
double pullStrengthMultiplier;
if (rangeModeObject.has("pullStrengthMultiplier"))
pullStrengthMultiplier = rangeModeObject.get("pullStrengthMultiplier").getAsDouble();
else {
defaultConfig = generateDefault();
pullStrengthMultiplier = defaultConfig.pullStrengthMultiplier;
}
boolean skipCanPickUpCheck;
if (rangeModeObject.has("skipCanPickUpCheck"))
skipCanPickUpCheck = rangeModeObject.get("skipCanPickUpCheck").getAsBoolean();
else {
if (defaultConfig == null) defaultConfig = generateDefault();
skipCanPickUpCheck = defaultConfig.skipCanPickUpCheck;
}


JsonObject onBreakModeObject = modesObject.get("OnBreak").getAsJsonObject();
boolean onBreakEnabled = onBreakModeObject.get("enabled").getAsBoolean();
String dropModeString = onBreakModeObject.get("dropLocation").getAsString();
if (!isValidEnumValue(DropMode.class, dropModeString))
throw new IllegalArgumentException("unknown dropLocation value");
DropMode dropMode = DropMode.valueOf(dropModeString);
boolean includeContainerItems = !onBreakModeObject.has("includeContainerItems") || onBreakModeObject.get("includeContainerItems").getAsBoolean();

MagnetCommandConfig config = new MagnetCommandConfig(permissionLevel, rangeEnabled, range, moveMode, onBreakEnabled, dropMode, includeContainerItems);
boolean includeContainerItems;
if (onBreakModeObject.has("includeContainerItems"))
includeContainerItems = onBreakModeObject.get("includeContainerItems").getAsBoolean();
else {
if (defaultConfig == null) defaultConfig = generateDefault();
includeContainerItems = defaultConfig.includeContainerItems;
}

MagnetCommandConfig config = new MagnetCommandConfig(
permissionLevel,
rangeEnabled,
range,
moveMode,
onBreakEnabled,
dropMode,
includeContainerItems,
pullStrengthMultiplier,
skipCanPickUpCheck
);
save(config);
return config;

Expand All @@ -100,7 +135,17 @@ public static MagnetCommandConfig load() {
}

static MagnetCommandConfig generateDefault() {
return new MagnetCommandConfig(2, true, 3, MoveMode.Pull, true, DropMode.Block, true);
return new MagnetCommandConfig(
2,
true,
3,
MoveMode.Pull,
true,
DropMode.Block,
true,
1,
false
);
}

private static <E extends Enum<E>> boolean isValidEnumValue(Class<E> enumType, String value) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/keesmand/magnetcommand/util/Magnet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.keesmand.magnetcommand.util;

import dev.keesmand.magnetcommand.MagnetCommandMod;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
Expand All @@ -16,6 +17,7 @@
public class Magnet {
public static void PullItem(Vec3d playerPos, ItemEntity item, double pullStrength) {
Vec3d itemPos = item.getPos();
pullStrength *= MagnetCommandMod.CONFIG.pullStrengthMultiplier;
item.addVelocity(
force(playerPos.x - itemPos.x, pullStrength),
force(playerPos.y - itemPos.y, pullStrength),
Expand All @@ -35,6 +37,7 @@ private static double force(double distance, double strength) {
}

public static boolean TestItemEntity(ItemEntity item) {
if (MagnetCommandMod.CONFIG.skipCanPickUpCheck) return true;
return !item.cannotPickup();
}

Expand Down

0 comments on commit 25d7b23

Please sign in to comment.