Skip to content

Commit

Permalink
Improve stability of rolling
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsupermanhd committed Jun 11, 2023
1 parent f2f3fbc commit 68bc23f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ yarn_mappings=1.19.4+build.1
loader_version=0.14.18

# Mod Properties
mod_version=1.3.7
mod_version=1.3.8
maven_group=maxsuperman.addons
archives_base_name=villager-roller

Expand Down
70 changes: 44 additions & 26 deletions src/main/java/maxsuperman/addons/roller/modules/VillagerRoller.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ public class VillagerRoller extends Module {

private final Setting<Integer> failedToPlaceDelay = sgGeneral.add(new IntSetting.Builder()
.name("place-fail-delay")
.description("Delay after failed block place")
.defaultValue(20*5)
.description("Delay after failed block place (milliseconds)")
.defaultValue(1500)
.min(0)
.sliderRange(0, 20*45)
.sliderRange(0, 10000)
.build());

private final Setting<Boolean> failedToPlaceDisable = sgGeneral.add(new BoolSetting.Builder()
Expand Down Expand Up @@ -143,7 +143,7 @@ public enum State {
public Block rollingBlock;
public VillagerEntity rollingVillager;
public List<rollingEnchantment> searchingEnchants = new ArrayList<>();
private int failedToPlaceDelayLeft = 0;
private long failedToPlacePrevMsg = System.currentTimeMillis();

public VillagerRoller() {
super(Categories.Misc, "villager-roller", "Rolls trades.");
Expand Down Expand Up @@ -230,7 +230,9 @@ public boolean saveSearchingToFile(File f) {
}
NbtCompound c = new NbtCompound();
c.put("rolling", l);
f.getParentFile().mkdirs();
if (!f.getParentFile().mkdirs()) {
return false;
}
try {
NbtIo.write(c, f);
} catch (IOException e) {
Expand Down Expand Up @@ -557,10 +559,20 @@ private void onStartBreakingBlockEvent(StartBreakingBlockEvent event) {
}
}

private void placeFailed(String msg) {
if (failedToPlacePrevMsg + failedToPlaceDelay.get() <= System.currentTimeMillis()) {
info(msg);
failedToPlacePrevMsg = System.currentTimeMillis();
}
if (failedToPlaceDisable.get()) {
toggle();
}
}

@EventHandler
private void onTick(TickEvent.Pre event) {
assert mc.world != null;
if (currentState == State.RollingBreakingBlock) {
assert mc.world != null;
if (mc.world.getBlockState(rollingBlockPos) == Blocks.AIR.getDefaultState()) {
// info("Block is broken, waiting for villager to clean profession...");
currentState = State.RollingWaitingForVillagerProfessionClear;
Expand All @@ -571,35 +583,41 @@ private void onTick(TickEvent.Pre event) {
}
}
} else if (currentState == State.RollingWaitingForVillagerProfessionClear) {
if (mc.world.getBlockState(rollingBlockPos).isOf(Blocks.LECTERN)) {
info("Rolling block mining reverted?");
currentState = State.RollingBreakingBlock;
return;
}
if (rollingVillager.getVillagerData().getProfession() == VillagerProfession.NONE) {
// info("Profession cleared");
currentState = State.RollingPlacingBlock;
}
} else if (currentState == State.RollingPlacingBlock) {
FindItemResult item = InvUtils.findInHotbar(rollingBlock.asItem());
if (!item.found()) {
placeFailed("Lectern not found in hotbar");
return;
}
if (!BlockUtils.canPlace(rollingBlockPos, true)) {
placeFailed("Can't place lectern");
return;
}
if (!BlockUtils.place(rollingBlockPos, item, headRotateOnPlace.get(), 5)) {
if (failedToPlaceDisable.get()) {
info("Failed to place block, roller disabled");
toggle();
} else {
if (failedToPlaceDelayLeft <= 0) {
info("Failed to place block, please place it manually");
failedToPlaceDelayLeft += failedToPlaceDelay.get();
} else {
failedToPlaceDelayLeft--;
}
}
} else {
currentState = State.RollingWaitingForVillagerProfessionNew;
placeFailed("Failed to place lectern");
return;
}
// } else if(currentState == State.RollingPlacingBlockRetry) {
// if(retryPlaceIn > 0) {
// retryPlaceIn--;
// } else {
// info("Trying to place block again");
// currentState = State.RollingPlacingBlock;
// }
currentState = State.RollingWaitingForVillagerProfessionNew;
} else if (currentState == State.RollingWaitingForVillagerProfessionNew) {
if (mc.world.getBlockState(rollingBlockPos) == Blocks.AIR.getDefaultState()) {
info("Lectern placement reverted by server (AC?)");
currentState = State.RollingPlacingBlock;
return;
}
if (!mc.world.getBlockState(rollingBlockPos).isOf(Blocks.LECTERN)) {
info("Placed wrong block?!");
currentState = State.RollingBreakingBlock;
return;
}
if (rollingVillager.getVillagerData().getProfession() != VillagerProfession.NONE) {
currentState = State.RollingWaitingForVillagerTrades;
triggerInteract();
Expand Down

0 comments on commit 68bc23f

Please sign in to comment.