Files
src
build.gradle
gradle.properties
settings.gradle
Code
22 393
src/main/java/com/policestatesim/PoliceStateSim.java
package com.policestatesim;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.AxeItem;
import net.minecraft.item.BowItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.SwordItem;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
public final class PoliceStateSim implements ModInitializer {
public static final String ID = "policestatesim";
public static final Identifier OPEN = id("open_official"), ACTION = id("official_action");
public static final Item RUBLE = register("state_ruble", new Item(new Item.Settings()));
public static final Item LOGGING = register("logging_license", new LicenseItem());
public static final Item HUNTING = register("hunting_license", new LicenseItem());
public static final Item MINING = register("mining_license", new LicenseItem());
public static final Item WEAPON = register("weapon_license", new LicenseItem());
private static final Item[] LICENSES = { LOGGING, HUNTING, MINING, WEAPON };
public static Identifier id(String path) { return new Identifier(ID, path); }
private static Item register(String name, Item item) { return Registry.register(Registries.ITEM, id(name), item); }
@Override public void onInitialize() {
Registry.register(Registries.ITEM_GROUP, id("items"), FabricItemGroup.builder()
.displayName(Text.translatable("itemGroup.policestatesim.items"))
.icon(() -> new ItemStack(RUBLE)).entries((context, entries) -> {
entries.add(RUBLE); for (Item license : LICENSES) entries.add(license);
}).build());
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
var world = server.getOverworld();
Complex.generate(world, StateData.get(world));
});
PlayerBlockBreakEvents.BEFORE.register((world, player, pos, state, blockEntity) -> {
if (world == world.getServer().getOverworld() && Complex.protectedPos(StateData.get(world.getServer().getOverworld()), pos)) {
player.sendMessage(Text.literal("The Government Complex is protected.").formatted(Formatting.RED), false);
return false;
}
if ((state.isIn(BlockTags.LOGS) && !has(player, LOGGING)) || (ore(state) && !has(player, MINING))) {
crime(player); return false;
}
return true;
});
AttackEntityCallback.EVENT.register((player, world, hand, target, hit) -> {
if (world.isClient) return ActionResult.PASS;
Item held = player.getStackInHand(hand).getItem();
if ((isHunted(target) && !has(player, HUNTING)) ||
((held instanceof SwordItem || held instanceof AxeItem || held instanceof BowItem) && !has(player, WEAPON))) {
crime(player); return ActionResult.FAIL;
}
return ActionResult.PASS;
});
UseEntityCallback.EVENT.register((player, world, hand, target, hit) -> {
if (!target.getCommandTags().contains("policestatesim_official")) return ActionResult.PASS;
if (world.isClient) return ActionResult.PASS;
if (player instanceof ServerPlayerEntity serverPlayer) {
PacketByteBuf buf = PacketByteBufs.create(); buf.writeInt(target.getId());
ServerPlayNetworking.send(serverPlayer, OPEN, buf);
}
return ActionResult.SUCCESS;
});
ServerPlayNetworking.registerGlobalReceiver(ACTION, (server, player, handler, buf, responseSender) -> {
int officialId = buf.readInt(); int action = buf.readInt();
server.execute(() -> transact(player, officialId, action));
});
}
public static boolean isHunted(Entity entity) {
EntityType<?> type = entity.getType();
return type == EntityType.COW || type == EntityType.PIG || type == EntityType.SHEEP || type == EntityType.CHICKEN;
}
public static boolean has(PlayerEntity player, Item item) {
PlayerInventory inventory = player.getInventory();
for (int i = 0; i < inventory.size(); i++) {
ItemStack stack = inventory.getStack(i);
if (stack.isOf(item) && stack.getDamage() < 300) return true;
}
return false;
}
public static void crime(PlayerEntity player) {
player.addCommandTag("wanted");
player.sendMessage(Text.literal("[POLICE] CRIME DETECTED! You are now WANTED!").formatted(Formatting.RED, Formatting.BOLD), false);
}
private static boolean ore(BlockState state) {
return state.isIn(BlockTags.COAL_ORES) || state.isIn(BlockTags.IRON_ORES) || state.isIn(BlockTags.COPPER_ORES)
|| state.isIn(BlockTags.GOLD_ORES) || state.isIn(BlockTags.DIAMOND_ORES)
|| state.isIn(BlockTags.REDSTONE_ORES) || state.isIn(BlockTags.EMERALD_ORES);
}
private static int count(PlayerInventory inv, Item item) {
int count = 0;
for (int i = 0; i < 36; i++) if (inv.getStack(i).isOf(item)) count += inv.getStack(i).getCount();
return count;
}
private static void take(PlayerInventory inv, Item item, int amount) {
for (int i = 0; i < 36 && amount > 0; i++) {
ItemStack stack = inv.getStack(i);
if (stack.isOf(item)) { int n = Math.min(amount, stack.getCount()); stack.decrement(n); amount -= n; }
}
}
private static boolean room(PlayerInventory inv, Item item, int amount) {
for (int i = 0; i < 36; i++) {
ItemStack stack = inv.getStack(i);
if (stack.isEmpty()) amount -= item.getMaxCount();
else if (stack.isOf(item) && item == RUBLE) amount -= stack.getMaxCount() - stack.getCount();
if (amount <= 0) return true;
}
return false;
}
private static void transact(ServerPlayerEntity player, int entityId, int action) {
Entity official = player.getServerWorld().getEntityById(entityId);
StateData data = StateData.get(player.getServer().getOverworld());
if (official == null || !official.getCommandTags().contains("policestatesim_official") ||
player.getServerWorld() != player.getServer().getOverworld() ||
!Complex.protectedPos(data, official.getBlockPos()) || player.squaredDistanceTo(official) > 64.0) return;
PlayerInventory inv = player.getInventory();
if (action == 0) {
if (data.claimed(player.getUuid())) { message(player, "Starter capital already claimed."); return; }
if (!room(inv, RUBLE, 50)) { message(player, "Not enough inventory space."); return; }
inv.insertStack(new ItemStack(RUBLE, 50)); data.claim(player.getUuid());
message(player, "Claimed 50 State Rubles.");
} else if (action == 1 || action == 2) {
Item sold = action == 1 ? Items.COBBLESTONE : Items.DIRT;
if (count(inv, sold) < 16) { message(player, "You need 16 of the selected block."); return; }
if (!room(inv, RUBLE, 5)) { message(player, "Not enough inventory space."); return; }
take(inv, sold, 16); inv.insertStack(new ItemStack(RUBLE, 5)); message(player, "Sold 16 blocks for 5 State Rubles.");
} else if (action >= 3 && action <= 6) {
if (count(inv, RUBLE) < 15) { message(player, "You need 15 State Rubles."); return; }
Item license = LICENSES[action - 3];
if (!room(inv, license, 1)) { message(player, "Not enough inventory space."); return; }
take(inv, RUBLE, 15); inv.insertStack(new ItemStack(license)); message(player, "License purchased for 15 State Rubles.");
}
}
private static void message(PlayerEntity player, String message) { player.sendMessage(Text.literal(message), false); }
}