Skip to content

Commit 6c9abe2

Browse files
committed
made that the constants cannot be changed, modified the init function
1 parent 23b9f46 commit 6c9abe2

6 files changed

Lines changed: 43 additions & 26 deletions

File tree

src/main/java/redart15/commandly/CommandlyMod.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
public class CommandlyMod implements ModInitializer, RecipeEntrypoint, GameStartEntrypoint {
1717
public static final String MOD_ID = "commandly";
1818
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
19-
public static final int MASK = 7;
20-
public static final int MAX_BLOCK_COUNT = 256 * 16 * 16 * 25;
19+
private static final int MASK = 7;
20+
private static final int MAX_BLOCK_COUNT = 256 * 16 * 16 * 25;
2121
public static final GameRuleBoolean MOSS_SPREADING = GameRules.register(new GameRuleBoolean("doMossSpreading", true));
2222
public static final GameRuleBoolean GRASS_SPREADING = GameRules.register(new GameRuleBoolean("doGrassSpreading", true));
2323
public static final GameRuleBoolean VEINMINING = GameRules.register(new GameRuleBoolean("veinmining", false));
@@ -50,6 +50,9 @@ private static void initialize(EntrypointContainer<CommandlyPlugin> plugin) {
5050
entrypoint.registerPickaxe(PickAxeRegister.getInstance());
5151
}
5252

53+
public static int getMask(){return MASK;}
54+
public static int getMaxBlockCount(){return MAX_BLOCK_COUNT;}
55+
5356
@Override
5457
public void onRecipesReady() {
5558
// no need

src/main/java/redart15/commandly/command/CommandProtect.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.function.Predicate;
2424

2525
import static redart15.commandly.command.CommandlyCommands.ReturnValues.*;
26-
import static redart15.commandly.CommandlyMod.MASK;
2726

2827
@SuppressWarnings("ALL") //cause this drives me nuts
2928
public class CommandProtect implements CommandManager.CommandRegistry {
@@ -98,7 +97,7 @@ private static int chunks_protect(CommandSource source, World world, Predicate<B
9897
Block<?> block = world.getBlock(fx + x, y, fz + z);
9998
if (block == null) continue;
10099
if (treecapitator.test(block) || veinmining.test(block)) {
101-
world.setBlockMetadata(fx + x, y, fz + z, (1 << MASK));
100+
world.setBlockMetadata(fx + x, y, fz + z, (1 << CommandlyMod.getMask()));
102101
count_protected++;
103102
}
104103
}
@@ -157,7 +156,7 @@ private static int radius_protect(
157156
throw new RuntimeException(e);
158157
}
159158

160-
if ((int) Math.floor(1.25 * Math.PI * Math.pow(radius, 3)) > CommandlyMod.MAX_BLOCK_COUNT) {
159+
if ((int) Math.floor(1.25 * Math.PI * Math.pow(radius, 3)) > CommandlyMod.getMaxBlockCount()) {
161160
source.sendTranslatableMessage("commadly.protected.tolarge");
162161
return code(CANNOT);
163162
}
@@ -169,7 +168,7 @@ private static int radius_protect(
169168
Block<?> block = world.getBlock(fx + x, fy + y, fz + z);
170169
if (block == null) continue;
171170
if (treecapitator.test(block) || veinmining.test(block)) {
172-
world.setBlockMetadata(fx + x, fy + y, fz + z, (1 << MASK));
171+
world.setBlockMetadata(fx + x, fy + y, fz + z, (1 << CommandlyMod.getMask()));
173172
count_protected++;
174173
}
175174
}
@@ -229,7 +228,7 @@ public static int point_protect(
229228
} catch (CommandSyntaxException e) {
230229
throw new RuntimeException(e);
231230
}
232-
if (Math.abs(fx - sx) * Math.abs(fy - sy) * Math.abs(fz - sz) > CommandlyMod.MAX_BLOCK_COUNT) {
231+
if (Math.abs(fx - sx) * Math.abs(fy - sy) * Math.abs(fz - sz) > CommandlyMod.getMaxBlockCount()) {
233232
source.sendTranslatableMessage("commadly.protected.toolarge");
234233
return code(CANNOT);
235234
}
@@ -241,7 +240,7 @@ public static int point_protect(
241240
Block<?> block = world.getBlock(x, y, z);
242241
if (block == null) continue;
243242
if (treecapitator.test(block) || veinmining.test(block)) {
244-
world.setBlockMetadata(x, y, z, (1 << MASK));
243+
world.setBlockMetadata(x, y, z, (1 << CommandlyMod.getMask()));
245244
count_protected++;
246245
}
247246
}

src/main/java/redart15/commandly/mixins/treecapitator/SmartTreeCapitator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import org.spongepowered.asm.mixin.Shadow;
1414
import org.spongepowered.asm.mixin.Unique;
1515
import org.spongepowered.asm.mixin.injection.At;
16+
import redart15.commandly.CommandlyMod;
1617

17-
import static redart15.commandly.CommandlyMod.MASK;
1818

1919
@Mixin(value = TreecapitatorHelper.class, remap = false)
2020
public abstract class SmartTreeCapitator {
@@ -30,7 +30,7 @@ public boolean isSmartLog(
3030
TreecapitatorHelper asThis = (TreecapitatorHelper) (Object) this;
3131
ChunkPosition p = asThis.basePosition;
3232
int metadata = asThis.world.getBlockMetadata(p.x, p.y, p.z);
33-
return block != null && !(block.getLogic() instanceof IPaintable) && (metadata >> MASK) == 0 && original.call(instance, block);
33+
return block != null && !(block.getLogic() instanceof IPaintable) && (metadata >> CommandlyMod.getMask()) == 0 && original.call(instance, block);
3434
}
3535

3636

@@ -52,6 +52,6 @@ private int getBlockID_addLogsAroundBlock(World instance, int x, int y, int z, O
5252

5353
@Unique
5454
private static boolean isSmartTreecapitator(@NotNull BlockLogic logic, int metadata) {
55-
return logic instanceof IPaintable || (metadata >> MASK) == 1;
55+
return logic instanceof IPaintable || (metadata >> CommandlyMod.getMask()) == 1;
5656
}
5757
}

src/main/java/redart15/commandly/veincapitator/OreGroups.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
/// Ore -> Group -> List of Group members
1313
public class OreGroups {
14-
protected static final OreGroups instance = new OreGroups();
14+
protected static OreGroups instance;
1515
protected static final Map<String, HashSet<NamespaceID>> GROUP_TO_BLOCKS = new HashMap<>();
1616
protected static final Map<NamespaceID, String> BLOCKS_TO_GROUP = new HashMap<>();
1717

@@ -39,7 +39,10 @@ public OreGroupBuilder addOre(Block<?> block) {
3939
}
4040

4141
public static void init() {
42-
// no need
42+
if(instance == null){
43+
instance = new OreGroups();
44+
}
45+
instance.load();
4346
}
4447

4548
public static OreGroups getInstance(){
@@ -50,9 +53,7 @@ public static OreGroupBuilder register(String string){
5053
return new OreGroupBuilder(string);
5154
}
5255

53-
protected OreGroups() {
54-
this.load();
55-
}
56+
private OreGroups() {}
5657

5758
private void load() {
5859
OreGroups.register("coal")

src/main/java/redart15/commandly/veincapitator/PickAxeRegister.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
import net.minecraft.core.item.Item;
44
import net.minecraft.core.item.material.ToolMaterial;
55
import net.minecraft.core.item.tool.ItemTool;
6+
import org.jetbrains.annotations.NotNull;
67

78
import java.lang.reflect.Method;
89
import java.util.HashMap;
910
import java.util.Map;
1011

1112
public class PickAxeRegister {
12-
protected static final PickAxeRegister instance = new PickAxeRegister();
13+
protected static PickAxeRegister instance;
1314
protected static final Map<Integer, ToolMaterial> toolSet = new HashMap<>();
1415

1516
public static void init() {
16-
// loads the class
17+
if (instance == null) {
18+
instance = new PickAxeRegister();
19+
}
20+
instance.load();
21+
}
22+
23+
private void load() {
24+
// nothign to load
1725
}
1826

1927
protected PickAxeRegister() {
@@ -27,18 +35,24 @@ public static boolean containsID(int id) {
2735
return toolSet.containsKey(id);
2836
}
2937

30-
public static boolean register(ItemTool pickaxe) {
31-
if (pickaxe == null) {
38+
public static boolean register(@NotNull Item pickaxe) {
39+
if (pickaxe instanceof ItemTool) {
40+
toolSet.put(pickaxe.id, ((ItemTool) pickaxe).getMaterial());
41+
}
42+
try {
43+
Method method = pickaxe.getClass().getMethod("getMaterial");
44+
ToolMaterial toolMaterial = (ToolMaterial) method.invoke(pickaxe);
45+
toolSet.put(pickaxe.id, toolMaterial);
46+
return true;
47+
} catch (NoSuchMethodException e) {
3248
return false;
49+
} catch (Exception e) {
50+
e.printStackTrace();
3351
}
34-
toolSet.put(pickaxe.id, pickaxe.getMaterial());
3552
return true;
3653
}
3754

38-
public static boolean register(Item pickaxe, ToolMaterial material) {
39-
if (pickaxe == null || material == null) {
40-
return false;
41-
}
55+
public static boolean register(@NotNull Item pickaxe, @NotNull ToolMaterial material) {
4256
toolSet.put(pickaxe.id, material);
4357
return true;
4458
}

src/main/java/redart15/commandly/veincapitator/VeinMining.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private static boolean isSmartMiner(@NotNull Block<?> block, int metadata) {
148148
if(!CommandlyConfig.SMART_VEINMINER){
149149
return false;
150150
}
151-
return block.getLogic() instanceof IPaintable || (metadata >> CommandlyMod.MASK) == 1;
151+
return block.getLogic() instanceof IPaintable || (metadata >> CommandlyMod.getMask()) == 1;
152152
}
153153

154154
private boolean languageKeyOre(Block<?> block) {

0 commit comments

Comments
 (0)