port to fabric
This commit is contained in:
247
src/main/java/quimufu/structure_item/MyItem.java
Normal file
247
src/main/java/quimufu/structure_item/MyItem.java
Normal file
@@ -0,0 +1,247 @@
|
||||
package quimufu.structure_item;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.mojang.datafixers.Dynamic;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.item.TooltipContext;
|
||||
import net.minecraft.datafixer.NbtOps;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemGroup;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.ItemUsageContext;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.MessageType;
|
||||
import net.minecraft.network.packet.s2c.play.TitleS2CPacket;
|
||||
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.structure.Structure;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.text.TranslatableText;
|
||||
import net.minecraft.util.*;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.registry.DefaultedRegistry;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static quimufu.structure_item.StructureItemMod.LOGGER;
|
||||
|
||||
public class MyItem extends Item {
|
||||
static Item.Settings p = (new Item.Settings()).group(ItemGroup.REDSTONE).maxCount(1);
|
||||
|
||||
public MyItem() {
|
||||
super(p);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendTooltip(ItemStack itemStack, World world, List<Text> texts, TooltipContext tooltipFlag) {
|
||||
if (!itemStack.hasTag() || !itemStack.getTag().contains("structure", 8)) {
|
||||
texts.add((new TranslatableText("item.structure_item.item.tooltip.tag.invalid")).formatted(Formatting.RED));
|
||||
} else {
|
||||
CompoundTag tag = itemStack.getTag();
|
||||
if (tooltipFlag.isAdvanced()) {
|
||||
texts.add(new TranslatableText("item.structure_item.item.tooltip.structure"));
|
||||
texts.add(new LiteralText(" " + tag.getString("structure")));
|
||||
if (tag.contains("allowedOn", 8)) {
|
||||
texts.add(new TranslatableText("item.structure_item.item.tooltip.allowed.on"));
|
||||
texts.add(new LiteralText(" " + tag.getString("allowedOn")));
|
||||
}
|
||||
if (tag.contains("offset")) {
|
||||
BlockPos offset = BlockPos.deserialize(new Dynamic<Tag>(NbtOps.INSTANCE, tag.get("offset")));
|
||||
texts.add(new TranslatableText("item.structure_item.item.tooltip.fixed.offset"));
|
||||
Text c = new TranslatableText("item.structure_item.item.tooltip.xyz",
|
||||
new LiteralText(String.valueOf(offset.getX())),
|
||||
new LiteralText(String.valueOf(offset.getY())),
|
||||
new LiteralText(String.valueOf(offset.getZ())));
|
||||
texts.add(c);
|
||||
} else {
|
||||
texts.add(new TranslatableText("item.structure_item.item.tooltip.dynamic.offset"));
|
||||
}
|
||||
if (tag.contains("blacklist", 9)) {
|
||||
texts.add(new TranslatableText("item.structure_item.item.tooltip.blacklist"));
|
||||
ListTag bl = tag.getList("blacklist", 8);
|
||||
int i = 0;
|
||||
for ( Tag entry : bl) {
|
||||
texts.add(new LiteralText(" " + entry.asString()));
|
||||
i++;
|
||||
if (i == 4) {
|
||||
texts.add(new TranslatableText("item.structure_item.item.tooltip.blacklist.more",
|
||||
new LiteralText(String.valueOf(bl.size() - i))));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionResult useOnBlock(ItemUsageContext c) {
|
||||
if (!c.getWorld().isClient) {
|
||||
ServerPlayerEntity player;
|
||||
if (c.getPlayer() instanceof ServerPlayerEntity) {
|
||||
player = ((ServerPlayerEntity) c.getPlayer());
|
||||
} else {
|
||||
player = null;
|
||||
}
|
||||
CompoundTag tag = c.getStack().getTag();
|
||||
if (tag == null) {
|
||||
TranslatableText message =
|
||||
new TranslatableText("items.structure.spawner.no.tag");
|
||||
sendPlayerChat(player, message);
|
||||
return ActionResult.FAIL;
|
||||
}
|
||||
Block allowed = null;
|
||||
if (tag.contains("allowedOn", 8)) {
|
||||
String allowedOn = tag.getString("allowedOn");
|
||||
allowed = getBlock(allowedOn);
|
||||
if (allowed == null) {
|
||||
TranslatableText message =
|
||||
new TranslatableText("items.structure.spawner.invalid.block",
|
||||
new LiteralText(allowedOn));
|
||||
sendPlayerChat(player, message);
|
||||
return ActionResult.FAIL;
|
||||
}
|
||||
}
|
||||
Block current = c.getWorld().getBlockState(c.getBlockPos()).getBlock();
|
||||
if (allowed != null && !current.equals(allowed)) {
|
||||
TranslatableText message =
|
||||
new TranslatableText("items.structure.spawner.invalid.block.clicked",
|
||||
current.getName(), allowed.getName());
|
||||
sendPlayer(player, message);
|
||||
return ActionResult.FAIL;
|
||||
}
|
||||
if (!tag.contains("structure", 8)) {
|
||||
LOGGER.info("No structure name set");
|
||||
TranslatableText message =
|
||||
new TranslatableText("items.structure.spawner.no.structure");
|
||||
sendPlayerChat(player, message);
|
||||
return ActionResult.FAIL;
|
||||
}
|
||||
String structureName = tag.getString("structure");
|
||||
Identifier structureResourceID = Identifier.tryParse(structureName);
|
||||
if (structureResourceID == null) {
|
||||
TranslatableText message =
|
||||
new TranslatableText("items.structure.spawner.invalid.structure.name");
|
||||
sendPlayerChat(player, message);
|
||||
return ActionResult.FAIL;
|
||||
}
|
||||
Structure x = ((ServerWorld) c.getWorld()).getStructureManager().getStructure(structureResourceID);
|
||||
if (x == null) {
|
||||
TranslatableText message =
|
||||
new TranslatableText("items.structure.spawner.structure.nonexistent",
|
||||
new LiteralText(structureResourceID.toString()));
|
||||
sendPlayerChat(player, message);
|
||||
return ActionResult.FAIL;
|
||||
}
|
||||
|
||||
BlockPos loc = c.getBlockPos().offset(c.getSide());
|
||||
if (tag.contains("offset")) {
|
||||
BlockPos offset = BlockPos.deserialize(new Dynamic<Tag>(NbtOps.INSTANCE, tag.get("offset")));
|
||||
loc = loc.add(offset);
|
||||
} else if (c.getPlayer() != null) {
|
||||
Direction direction = Direction.getEntityFacingOrder(c.getPlayer())[0];
|
||||
BlockPos size = x.getSize();
|
||||
loc = loc.add(getDirectionalOffset(direction, size));
|
||||
} else {
|
||||
LOGGER.info("No player & no offset");
|
||||
}
|
||||
|
||||
MyPlacementSettings ps = (new MyPlacementSettings());
|
||||
if (tag.contains("blacklist", 9)) {
|
||||
ListTag bl = tag.getList("blacklist", 8);
|
||||
List<Block> blacklist = Lists.newArrayList();
|
||||
for (Tag b : bl) {
|
||||
Block block = getBlock(b.asString());
|
||||
if (block != null) {
|
||||
blacklist.add(block);
|
||||
} else {
|
||||
TranslatableText message =
|
||||
new TranslatableText("items.structure.spawner.invalid.block",
|
||||
new LiteralText(b.asString()));
|
||||
sendPlayerChat(player, message);
|
||||
}
|
||||
|
||||
}
|
||||
ps.forbidOverwrite(blacklist);
|
||||
}
|
||||
ps.setWorld(c.getWorld())
|
||||
.setSize(x.getSize())
|
||||
.setMirrored(BlockMirror.NONE)
|
||||
.setRotation(BlockRotation.NONE)
|
||||
.setChunkPosition(null);
|
||||
boolean succes = x.method_15172(c.getWorld(), loc, ps, 2);
|
||||
if (succes) {
|
||||
c.getStack().decrement(1);
|
||||
return ActionResult.SUCCESS;
|
||||
}
|
||||
TranslatableText message =
|
||||
new TranslatableText("items.structure.spawner.invalid.location");
|
||||
sendPlayer(player, message);
|
||||
return ActionResult.FAIL;
|
||||
}
|
||||
return ActionResult.FAIL;
|
||||
}
|
||||
|
||||
private static void sendPlayer(ServerPlayerEntity player, Text message) {
|
||||
if (player == null)
|
||||
return;
|
||||
ServerPlayNetworkHandler connection = player.networkHandler;
|
||||
TitleS2CPacket packet = new TitleS2CPacket(TitleS2CPacket.Action.SUBTITLE, message);
|
||||
connection.sendPacket(packet);
|
||||
packet = new TitleS2CPacket(TitleS2CPacket.Action.TITLE, new LiteralText(""));
|
||||
connection.sendPacket(packet);
|
||||
}
|
||||
|
||||
private static void sendPlayerChat(ServerPlayerEntity player, Text message) {
|
||||
if (player != null)
|
||||
player.sendChatMessage(message, MessageType.SYSTEM);
|
||||
LOGGER.info(message.asFormattedString());
|
||||
}
|
||||
|
||||
private BlockPos getDirectionalOffset(Direction direction, BlockPos size) {
|
||||
BlockPos loc = new BlockPos(0, 0, 0);
|
||||
switch (direction) {
|
||||
case WEST:
|
||||
loc = loc.offset(Direction.NORTH, size.getZ() / 2);
|
||||
loc = loc.offset(Direction.WEST, size.getX() - 1);
|
||||
break;
|
||||
case EAST: //positive x
|
||||
loc = loc.offset(Direction.NORTH, size.getZ() / 2);
|
||||
break;
|
||||
case NORTH:
|
||||
loc = loc.offset(Direction.NORTH, size.getZ() - 1);
|
||||
loc = loc.offset(Direction.WEST, size.getX() / 2);
|
||||
break;
|
||||
case SOUTH: //positive z
|
||||
loc = loc.offset(Direction.WEST, size.getX() / 2);
|
||||
break;
|
||||
case UP: //positive y
|
||||
loc = loc.offset(Direction.NORTH, size.getZ() / 2);
|
||||
loc = loc.offset(Direction.WEST, size.getX() / 2);
|
||||
loc = loc.offset(Direction.UP);
|
||||
break;
|
||||
case DOWN:
|
||||
loc = loc.offset(Direction.NORTH, size.getZ() / 2);
|
||||
loc = loc.offset(Direction.WEST, size.getX() / 2);
|
||||
loc = loc.offset(Direction.DOWN, size.getY());
|
||||
break;
|
||||
}
|
||||
return loc;
|
||||
}
|
||||
|
||||
private Block getBlock(String loc) {
|
||||
Identifier location = Identifier.tryParse(loc);
|
||||
DefaultedRegistry<Block> blocks = Registry.BLOCK;
|
||||
if (location == null || !blocks.containsId(location)) {
|
||||
return null;
|
||||
}
|
||||
return blocks.get(location);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package quimufu.structure_item;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.structure.Structure;
|
||||
import net.minecraft.structure.StructurePlacementData;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Box;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MyPlacementSettings extends StructurePlacementData {
|
||||
private List<Block> blacklist;
|
||||
private World world;
|
||||
private BlockPos size;
|
||||
|
||||
public MyPlacementSettings forbidOverwrite(List<Block> blocks) {
|
||||
if (blocks.size() == 0) {
|
||||
blacklist = null;
|
||||
return this;
|
||||
}
|
||||
blacklist = Lists.newArrayList(blocks);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyPlacementSettings setWorld(World w) {
|
||||
world = w;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyPlacementSettings setSize(BlockPos p) {
|
||||
size = p;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Structure.StructureBlockInfo> method_15121(List<List<Structure.StructureBlockInfo>> blocks, BlockPos pos) {
|
||||
if (world == null || pos == null || size == null) {
|
||||
return super.method_15121(blocks, pos);
|
||||
}
|
||||
|
||||
List<List<Structure.StructureBlockInfo>> eligibleStructures = new ArrayList<>();
|
||||
if (blacklist == null) {
|
||||
eligibleStructures = blocks;
|
||||
} else {
|
||||
for (List<Structure.StructureBlockInfo> struct : blocks) {
|
||||
if (isValid(struct, pos)) {
|
||||
eligibleStructures.add(struct);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (eligibleStructures.size() == 0)
|
||||
setIgnoreEntities(true);
|
||||
List<Structure.StructureBlockInfo> locs = super.method_15121(eligibleStructures, pos);
|
||||
if (!locs.isEmpty()) {
|
||||
List<Entity> entitiesWithinAABB = world.getNonSpectatingEntities(Entity.class, new Box(pos,pos.add(size)));
|
||||
for (Structure.StructureBlockInfo blockInfo : locs) {
|
||||
BlockPos posToClean = blockInfo.pos.add(pos);
|
||||
for (Entity e : entitiesWithinAABB) {
|
||||
e.getBoundingBox().contains(new Vec3d(posToClean));
|
||||
if (e.getBoundingBox().intersects(new Box(posToClean))) {
|
||||
e.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return locs;
|
||||
}
|
||||
|
||||
private boolean isValid(List<Structure.StructureBlockInfo> struct, BlockPos pos) {
|
||||
for (Structure.StructureBlockInfo bi : struct) {
|
||||
BlockPos posToCheck = bi.pos.add(pos);
|
||||
if (World.isValid(posToCheck)) {
|
||||
Block blockToCheck = world.getBlockState(posToCheck).getBlock();
|
||||
for (Block b : blacklist) {
|
||||
if (blockToCheck.equals(b)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
29
src/main/java/quimufu/structure_item/StructureItemMod.java
Normal file
29
src/main/java/quimufu/structure_item/StructureItemMod.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package quimufu.structure_item;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
||||
public class StructureItemMod implements ModInitializer {
|
||||
|
||||
public static Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
public static final String MOD_ID = "structure_item";
|
||||
public static final String MOD_NAME = "Structure Item Mod (Fabric)";
|
||||
public static final Item item = new MyItem();
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
log(Level.INFO, "Initializing");
|
||||
Registry.register(Registry.ITEM, MOD_ID + ":item", item);
|
||||
}
|
||||
|
||||
public static void log(Level level, String message) {
|
||||
LOGGER.log(level, "[" + MOD_NAME + "] " + message);
|
||||
}
|
||||
|
||||
}
|
||||
BIN
src/main/resources/assets/structure_item/icon.png
Normal file
BIN
src/main/resources/assets/structure_item/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
18
src/main/resources/assets/structure_item/lang/en_us.json
Normal file
18
src/main/resources/assets/structure_item/lang/en_us.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"items.structure.spawner.invalid.block.clicked": "Digga %s is net %s, maan.",
|
||||
"items.structure.spawner.invalid.location": "An irreplaceable block is in the way!",
|
||||
"items.structure.spawner.invalid.structure.name": "Invalid structure name",
|
||||
"items.structure.spawner.structure.nonexistent": "Structure: %s does not exist",
|
||||
"items.structure.spawner.invalid.block": "Block %s invalid",
|
||||
"items.structure.spawner.no.tag": "Item has no NBT tag",
|
||||
"items.structure.spawner.no.structure": "Item has no String \"structure\": tag",
|
||||
"item.structure_item.item": "Structure Spawner",
|
||||
"item.structure_item.item.tooltip.tag.invalid": "Invalid item!",
|
||||
"item.structure_item.item.tooltip.structure": "Places down: ",
|
||||
"item.structure_item.item.tooltip.allowed.on": "Can be placed on: ",
|
||||
"item.structure_item.item.tooltip.fixed.offset": "Offset:",
|
||||
"item.structure_item.item.tooltip.xyz":" x: %s y: %s z: %s",
|
||||
"item.structure_item.item.tooltip.dynamic.offset": "Dynamic offset",
|
||||
"item.structure_item.item.tooltip.blacklist": "Blacklist:",
|
||||
"item.structure_item.item.tooltip.blacklist.more": " And %s more..."
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"parent": "item/handheld",
|
||||
"textures": {
|
||||
"layer0": "item/stick"
|
||||
}
|
||||
}
|
||||
27
src/main/resources/fabric.mod.json
Normal file
27
src/main/resources/fabric.mod.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "structure_item",
|
||||
"version": "${version}",
|
||||
"name": "Structure Item Mod (Fabric)",
|
||||
"description": "This mod adds an item, that, with proper NBT might help you build a modpack... maybe",
|
||||
"authors": [
|
||||
"QuImUfu"
|
||||
],
|
||||
"contributors": [],
|
||||
"contact": {},
|
||||
"license": "MIT",
|
||||
"icon": "assets/structure_item/icon.png",
|
||||
"environment": "*",
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"quimufu.structure_item.StructureItemMod"
|
||||
],
|
||||
"client": [],
|
||||
"server": []
|
||||
},
|
||||
"mixins": [],
|
||||
"depends": {
|
||||
"fabricloader": ">=0.4.0",
|
||||
"fabric": "*"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user