From 195c9b7c082c0991b876044f31df92c1959a44c1 Mon Sep 17 00:00:00 2001 From: phinner <62483793+Phinner@users.noreply.github.com> Date: Tue, 10 Jan 2023 04:35:29 +0100 Subject: [PATCH] feat: prevent players from using logic on payload conveyors --- README.md | 19 +++++--- plugin.json | 2 +- .../patches/NoPayloadConveyorCrashPlugin.java | 48 +++++++++++++++++++ 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1237c58..9035308 100644 --- a/README.md +++ b/README.md @@ -3,20 +3,27 @@ [![Build status](https://github.com/Xpdustry/TemplatePlugin/actions/workflows/build.yml/badge.svg?branch=master&event=push)](https://github.com/Xpdustry/TemplatePlugin/actions/workflows/build.yml) [![Mindustry 7.0 ](https://img.shields.io/badge/Mindustry-7.0-ffd37f)](https://github.com/Anuken/Mindustry/releases) -This plugin prevents players from crashing a server by constantly changing the direction of a payload conveyor router. +This plugin prevents players from crashing a server by constantly changing the direction of a payload conveyor. +It accomplishes this by : -> Unlike this [mod](https://github.com/Agzam4/Mindustry-bugfixes-plugin), this plugin doesn't reload the entire game but -> uses reflection -> to change the broken code. It also preserves the binary compatibility with other plugins. +- Replacing the broken blocks with a patched version using reflection, unlike + this [mod](https://github.com/Agzam4/Mindustry-bugfixes-plugin) that does this by reloading the entire game. + +- Preventing players from binding logic processors to payload conveyors. ## Installation -This plugin requires : +### Requirements - Java 17 or above. - Mindustry v140 or above. +### Deployment + +Download the plugin jar in the [releases](https://github.com/Xpdustry/NoPayloadConveyorCrash/releases) and move it in +the `config/mods` directory of your server. + ## Building - `./gradlew shadowJar` to compile the plugin into a usable jar (will be located @@ -25,5 +32,3 @@ This plugin requires : - `./gradlew jar` for a plain jar that contains only the plugin code. - `./gradlew runMindustryServer` to run the plugin in a local Mindustry server. - -- `./gradlew test` to run the unit tests of the plugin. diff --git a/plugin.json b/plugin.json index 9e4e0f0..b63de0f 100644 --- a/plugin.json +++ b/plugin.json @@ -3,7 +3,7 @@ "displayName": "NoPayloadConveyorCrashPlugin", "author": "Xpdustry", "description": "No more payload conveyor crashes.", - "version": "1.0.0", + "version": "1.1.0", "minGameVersion": "140", "hidden": true, "java": true, diff --git a/src/main/java/fr/xpdustry/patches/NoPayloadConveyorCrashPlugin.java b/src/main/java/fr/xpdustry/patches/NoPayloadConveyorCrashPlugin.java index 63764d1..9c77ac1 100644 --- a/src/main/java/fr/xpdustry/patches/NoPayloadConveyorCrashPlugin.java +++ b/src/main/java/fr/xpdustry/patches/NoPayloadConveyorCrashPlugin.java @@ -25,7 +25,11 @@ */ package fr.xpdustry.patches; +import arc.Events; +import arc.math.geom.Point2; +import arc.struct.IntSet; import arc.struct.ObjectMap; +import arc.util.Log; import arc.util.Strings; import java.util.function.Consumer; import java.util.function.Function; @@ -33,13 +37,19 @@ import mindustry.content.Blocks; import mindustry.content.Items; import mindustry.ctype.MappableContent; +import mindustry.game.EventType; +import mindustry.game.EventType.Trigger; import mindustry.mod.Plugin; import mindustry.type.Category; import mindustry.type.ItemStack; import mindustry.world.Block; +import mindustry.world.blocks.logic.LogicBlock.LogicBuild; +import mindustry.world.blocks.payloads.PayloadConveyor.PayloadConveyorBuild; +@SuppressWarnings("unused") public final class NoPayloadConveyorCrashPlugin extends Plugin { + private final IntSet processorIndex = new IntSet(); private final ObjectMap[] contentNameMap; @SuppressWarnings("unchecked") @@ -82,6 +92,8 @@ public void init() { block.researchCostMultiplier = 4f; block.underBullets = true; }); + + this.setupLogicUnlink(); } private void replaceBlock( @@ -116,4 +128,40 @@ private void replaceBlock( throw new RuntimeException(e); } } + + private void setupLogicUnlink() { + Events.on(EventType.WorldLoadEvent.class, event -> { + processorIndex.clear(); + }); + + Events.on(EventType.TilePreChangeEvent.class, event -> { + processorIndex.remove(event.tile.pos()); + }); + + Events.on(EventType.TileChangeEvent.class, event -> { + if (event.tile.build instanceof LogicBuild building) { + processorIndex.add(building.pos()); + } + }); + + Events.run(Trigger.update, () -> { + final var iterator = processorIndex.iterator(); + while (iterator.hasNext) { + final var position = iterator.next(); + if (!(Vars.world.build(position) instanceof final LogicBuild processor)) { + Log.warn( + "[NoPayloadConveyorCrashPlugin] Logic processor at @ is not a LogicBuild instance!", + Point2.unpack(position)); + iterator.remove(); + continue; + } + for (final var link : processor.links) { + // de-configure the link if it is a payload conveyor + if (link.active && Vars.world.build(link.x, link.y) instanceof PayloadConveyorBuild) { + processor.configure(Point2.pack(link.x, link.y)); + } + } + } + }); + } }