Files
src
build.gradle
settings.gradle
Code
24 11,671
src/main/java/dev/kodari/retool/ReToolMod.java
package dev.kodari.retool;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;
import net.minecraft.util.ActionResult;
public class ReToolMod implements ModInitializer {
public static final Identifier GRAVITY_INPUT = new Identifier("retool", "gravity_input");
public static final Identifier OPEN_BUILDER = new Identifier("retool", "open_builder");
public static final Identifier BUILD = new Identifier("retool", "build");
public static final Item RETOOL = Registry.register(Registries.ITEM, new Identifier("retool", "retool"), new ReToolItem(new Item.Settings().maxCount(1)));
public static final Item GEAR = Registry.register(Registries.ITEM, new Identifier("retool", "retool_gear"), new ReToolGearItem(new Item.Settings().maxCount(1)));
@Override
public void onInitialize() {
ServerPlayNetworking.registerGlobalReceiver(BUILD, (server, player, handler, buf, sender) -> {
String block = buf.readString(256);
int shape = buf.readInt();
int size = buf.readInt();
server.execute(() -> BuilderController.build(player, block, shape, size));
});
ServerPlayNetworking.registerGlobalReceiver(GRAVITY_INPUT, (server, player, handler, buf, sender) -> {
int input = buf.readByte();
server.execute(() -> GravityController.input(player, input));
});
ServerTickEvents.END_SERVER_TICK.register(server -> {
GravityController.tick(server);
RopeController.tick();
((ReToolItem) RETOOL).tickSelections(server);
});
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
GravityController.release(handler.player, false);
RopeController.cancel(handler.player);
((ReToolItem) RETOOL).clearSelection(handler.player);
});
AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) ->
((ReToolItem) RETOOL).selectDuplicateBlock(player, world, hand, pos));
AttackEntityCallback.EVENT.register((player, world, hand, entity, hit) ->
((ReToolItem) RETOOL).selectDuplicateEntity(player, world, hand, entity));
UseBlockCallback.EVENT.register((player, world, hand, hit) -> {
if (!player.getStackInHand(hand).isOf(RETOOL) || player.isSpectator()) return ActionResult.PASS;
if (world.isClient) return ActionResult.SUCCESS;
((ReToolItem) RETOOL).use(world, player, hand);
return ActionResult.CONSUME;
});
UseEntityCallback.EVENT.register((player, world, hand, entity, hit) -> {
if (!player.getStackInHand(hand).isOf(RETOOL) || player.isSpectator()) return ActionResult.PASS;
if (world.isClient) return ActionResult.SUCCESS;
((ReToolItem) RETOOL).use(world, player, hand);
return ActionResult.CONSUME;
});
ItemGroupEvents.modifyEntriesEvent(RegistryKey.of(RegistryKeys.ITEM_GROUP, new Identifier("minecraft", "tools_and_utilities")))
.register(entries -> { entries.add(RETOOL); entries.add(GEAR); });
}
}