Skip to content

Commit e2f3ee7

Browse files
committed
creative mode parity
1 parent 8753b7d commit e2f3ee7

File tree

4 files changed

+84
-44
lines changed

4 files changed

+84
-44
lines changed

src/main/java/net/just_s/sds/Config.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@
2626
public class Config {
2727
private static final File configFile = FabricLoader.getInstance().getConfigDir().resolve("SDS.json").toFile();
2828

29-
public static String MESSAGE_nomodify;
30-
public static String MESSAGE_select;
31-
public static String MESSAGE_change;
32-
3329
public static boolean whitelist;
3430
private static HashMap<String, Boolean> properties = new HashMap<>();
3531

@@ -46,11 +42,6 @@ public static void load() {
4642

4743
whitelist = (boolean) jfile.get("whitelist");
4844

49-
JSONObject messages = (JSONObject) jfile.get("messages");
50-
MESSAGE_nomodify = (String) messages.get("nomodify");
51-
MESSAGE_select = (String) messages.get("select");
52-
MESSAGE_change = (String) messages.get("change");
53-
5445
JSONObject allowed = (JSONObject) jfile.get("allowed");
5546
JSONArray properties_allowed = (JSONArray) allowed.get("properties");
5647
JSONArray tags_allowed_js = (JSONArray) allowed.get("tags");
@@ -81,9 +72,6 @@ public static void load() {
8172

8273
private static void factorySettings() {
8374
whitelist = false;
84-
MESSAGE_nomodify = "This block is not modifiable.";
85-
MESSAGE_select = "Property «%s» was selected (%s).";
86-
MESSAGE_change = "Property «%s» was modified (%s).";
8775
properties = new HashMap<>();
8876
tags_allowed = new HashMap<>();
8977
tags_forbidden = new HashMap<>();
@@ -96,12 +84,6 @@ public static void save() {
9684
JSONObject jfile = new JSONObject();
9785
jfile.put("whitelist", whitelist);
9886

99-
JSONObject messages = new JSONObject();
100-
messages.put("nomodify", MESSAGE_nomodify);
101-
messages.put("select", MESSAGE_select);
102-
messages.put("change", MESSAGE_change);
103-
jfile.put("messages", messages);
104-
10587
JSONObject allowed = new JSONObject();
10688
JSONArray properties_allowed = new JSONArray();
10789
for (Map.Entry<String, Boolean> entry : properties.entrySet()) {

src/main/java/net/just_s/sds/SDSMod.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
package net.just_s.sds;
22

33
import net.fabricmc.api.ModInitializer;
4+
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
5+
import net.minecraft.component.ComponentChanges;
6+
import net.minecraft.component.DataComponentTypes;
7+
import net.minecraft.component.type.NbtComponent;
8+
import net.minecraft.item.DebugStickItem;
9+
import net.minecraft.item.ItemStack;
10+
import net.minecraft.item.Items;
11+
import net.minecraft.nbt.NbtCompound;
12+
import net.minecraft.nbt.NbtLong;
13+
import net.minecraft.util.ActionResult;
414
import org.slf4j.Logger;
515
import org.slf4j.LoggerFactory;
616

@@ -11,5 +21,50 @@ public class SDSMod implements ModInitializer {
1121
public void onInitialize() {
1222
Config.load();
1323
LOGGER.info("SDS initialized successfully!");
24+
AttackBlockCallback.EVENT.register(
25+
(player, world, hand, pos, direction) -> {
26+
// callback hooks before the spectator check
27+
if (player.isSpectator()) {
28+
return ActionResult.PASS;
29+
}
30+
31+
// check if player actually holds debug stick while punching
32+
ItemStack stack = player.getStackInHand(hand);
33+
if (!stack.isOf(Items.DEBUG_STICK)) {
34+
return ActionResult.PASS;
35+
}
36+
37+
// While in creative, you break every block with one click.
38+
// Original Debug Stick behavior hooks its "changing" ability
39+
// to a player breaking block.
40+
// We want to imitate this behaviour with a simple one-click punch
41+
42+
// But there is another problem:
43+
// AttackBlockCallback fires EVERY tick, which produces spam with "changing" ability
44+
// in other words, states cycle every tick while you are pressing left-click
45+
// This is not cool!
46+
47+
// So I decided to put custom nbt timer to prevent spamming
48+
// (It is still buggy, suggestions appreciated)
49+
NbtComponent component = stack.get(DataComponentTypes.CUSTOM_DATA);
50+
if (component != null) {
51+
NbtCompound nbtData = component.copyNbt();
52+
long lastModified = nbtData.getLong("LastModified");
53+
if (world.getTime() < lastModified + 5) {
54+
return ActionResult.PASS;
55+
}
56+
}
57+
58+
NbtCompound newNbtData = new NbtCompound();
59+
newNbtData.put("LastModified", NbtLong.of(world.getTime()));
60+
stack.applyChanges(
61+
ComponentChanges.builder()
62+
.add(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(newNbtData))
63+
.build()
64+
);
65+
stack.getItem().canMine(world.getBlockState(pos), world, pos, player);
66+
return ActionResult.PASS;
67+
}
68+
);
1469
}
1570
}

src/main/java/net/just_s/sds/mixin/DebugStickMixin.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.component.type.DebugStickStateComponent;
88
import net.minecraft.entity.player.PlayerEntity;
99
import net.minecraft.item.DebugStickItem;
10+
import net.minecraft.item.Item;
1011
import net.minecraft.item.ItemStack;
1112
import net.minecraft.registry.entry.RegistryEntry;
1213
import net.minecraft.state.StateManager;
@@ -24,7 +25,11 @@
2425
import java.util.Collection;
2526

2627
@Mixin(DebugStickItem.class)
27-
public class DebugStickMixin {
28+
public class DebugStickMixin extends Item {
29+
public DebugStickMixin(Settings settings) {
30+
super(settings);
31+
}
32+
2833
@Shadow
2934
private static void sendMessage(PlayerEntity player, Text message) {}
3035

@@ -52,8 +57,8 @@ private void onUSE(PlayerEntity player, BlockState state, WorldAccess world, Blo
5257
Collection<Property<?>> collection = stateManager.getProperties();
5358

5459
// check if block is modifiable by the config
55-
if (!isBlockAllowedToModify(state.getBlock()) || collection.isEmpty()) {
56-
sendMessage(player, Text.of(Config.MESSAGE_nomodify));
60+
if (!isBlockAllowedToModify(block) || collection.isEmpty()) {
61+
sendMessage(player, Text.translatable(this.getTranslationKey() + ".empty", new Object[]{registryEntry.getIdAsString()}));
5762
cir.setReturnValue(false);
5863
return;
5964
}
@@ -70,22 +75,7 @@ private void onUSE(PlayerEntity player, BlockState state, WorldAccess world, Blo
7075

7176
Property<?> property = stateComponent.properties().get(registryEntry);
7277

73-
if (player.isSneaking()) {
74-
// select next property
75-
property = getNextProperty(collection, property, block);
76-
// save chosen property in the NBT data of Debug Stick
77-
stack.set(DataComponentTypes.DEBUG_STICK_STATE, stateComponent.with(registryEntry, property));
78-
79-
// send the player a message of successful selecting
80-
sendMessage(player, Text.of(
81-
String.format(
82-
Config.MESSAGE_select,
83-
property.getName(),
84-
getValueString(state, property)
85-
)
86-
)
87-
);
88-
} else {
78+
if (update) {
8979
// change value of property
9080
if (property == null) {
9181
property = getNextProperty(collection, null, block);
@@ -96,12 +86,25 @@ private void onUSE(PlayerEntity player, BlockState state, WorldAccess world, Blo
9686
// update chosen block with its new state
9787
world.setBlockState(pos, newState, 18);
9888
// send the player a message of successful modifying
99-
sendMessage(player, Text.of(
100-
String.format(
101-
Config.MESSAGE_change,
102-
property.getName(),
103-
getValueString(newState, property)
104-
)
89+
sendMessage(
90+
player,
91+
Text.translatable(
92+
this.getTranslationKey() + ".update",
93+
new Object[]{property.getName(), getValueString(newState, property)}
94+
)
95+
);
96+
} else {
97+
// select next property
98+
property = getNextProperty(collection, property, block);
99+
// save chosen property in the NBT data of Debug Stick
100+
stack.set(DataComponentTypes.DEBUG_STICK_STATE, stateComponent.with(registryEntry, property));
101+
102+
// send the player a message of successful selecting
103+
sendMessage(
104+
player,
105+
Text.translatable(
106+
this.getTranslationKey() + ".select",
107+
new Object[]{property.getName(), getValueString(state, property)}
105108
)
106109
);
107110
}

src/main/resources/sds.mixins.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"required": true,
33
"minVersion": "0.8",
44
"package": "net.just_s.sds.mixin",
5-
"compatibilityLevel": "JAVA_17",
5+
"compatibilityLevel": "JAVA_21",
66
"mixins": [
77
"DebugStickMixin"
88
],

0 commit comments

Comments
 (0)