This commit is contained in:
TheBrokenRail 2020-08-24 21:28:06 -04:00
parent fa825173f9
commit dbcf8bb6d7
17 changed files with 237 additions and 109 deletions

View File

@ -17,9 +17,10 @@ minecraft {
} }
repositories { repositories {
maven { //maven {
url = uri("https://maven.thebrokenrail.com/") // url = uri("https://maven.thebrokenrail.com/")
} //}
mavenLocal()
} }
dependencies { dependencies {

View File

@ -3,9 +3,9 @@ org.gradle.jvmargs = -Xmx1G
# Fabric Properties # Fabric Properties
# check these on https://fabricmc.net/use # check these on https://fabricmc.net/use
minecraft_version = 1.16-pre3 minecraft_version = 1.16.2
yarn_build = 1 yarn_build = 21
fabric_loader_version = 0.8.7+build.201 fabric_loader_version = 0.9.2+build.206
# Mod Properties # Mod Properties
mod_version = 1.0.0 mod_version = 1.0.0

View File

@ -1,33 +1,12 @@
import { BlockSettings, Identifier, Registry, BlockState, ActionResult, World, Pos, Hand, PlayerEntity, BlockItem, ItemSettings, CustomItem, Direction, LivingEntity, CustomBlockWithEntity, CustomBlockEntity, CompoundTag, NumberType } from 'minecraft'; import { BlockSettings, Identifier, Registry, BlockState, ActionResult, World, Pos, Hand, PlayerEntity, BlockItem, ItemSettings, Direction, BuiltinRegistry } from 'minecraft';
console.log('hello'); console.log('hello');
class MyBlockEntity extends CustomBlockEntity { const ironBlock = BuiltinRegistry.BLOCK.get(new Identifier('minecraft:iron_block'));
ticks = 0;
toTag(tag: CompoundTag): CompoundTag {
tag.set('MyTicks', this.ticks, NumberType.INT);
return tag;
}
fromTag(tag: CompoundTag): void {
const obj = tag.get('MyTicks');
if (typeof obj === 'number') {
this.ticks = obj;
} else {
this.ticks = 0;
}
}
tick(): void {
this.ticks++;
this.markDirty();
}
}
class MyBlock extends CustomBlockWithEntity { class MyBlock extends CustomBlockWithEntity {
constructor() { constructor() {
super(new BlockSettings('STONE', 'IRON')); super(new BlockSettings(ironBlock.getMaterial(), ironBlock.getMaterialColor()));
} }
onUse(world: World, blockState: BlockState, blockPos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult { onUse(world: World, blockState: BlockState, blockPos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult {
@ -46,28 +25,3 @@ class MyBlock extends CustomBlockWithEntity {
Registry.register(Registry.BLOCK, new Identifier('modid', 'my_block'), new MyBlock()); Registry.register(Registry.BLOCK, new Identifier('modid', 'my_block'), new MyBlock());
Registry.register(Registry.ITEM, new Identifier('modid', 'my_block'), new BlockItem(new Identifier('modid', 'my_block'), new ItemSettings())); Registry.register(Registry.ITEM, new Identifier('modid', 'my_block'), new BlockItem(new Identifier('modid', 'my_block'), new ItemSettings()));
class MyItem extends CustomItem {
constructor() {
super(new ItemSettings().setItemGroup('building_blocks').setMaxCount(128));
}
onUse(world: World, player: PlayerEntity, hand: Hand): ActionResult {
console.log('Item Use: Normal');
return ActionResult.SUCCESS;
}
onUseOnBlock(world: World, blockPos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult {
console.log('Item Use: Block ' + blockPos.toString());
world.spawnEntity(new Identifier('minecraft:cow'), blockPos.offset(side).add(new Pos(0.5, 0, 0.5))).toString();
return ActionResult.SUCCESS;
}
onUseOnEntity(player: PlayerEntity, target: LivingEntity, hand: Hand): ActionResult {
console.log('Item Use: Entity ' + target.toTag().toString());
console.log('Health: ' + target.toTag().get('Health').toString());
return ActionResult.SUCCESS;
}
}
Registry.register(Registry.ITEM, new Identifier('modid', 'my_item'), new MyItem());

View File

@ -3,9 +3,9 @@ org.gradle.jvmargs = -Xmx1G
# Fabric Properties # Fabric Properties
# check these on https://fabricmc.net/use # check these on https://fabricmc.net/use
minecraft_version = 1.16-pre3 minecraft_version = 1.16.2
yarn_build = 1 yarn_build = 21
fabric_loader_version = 0.8.7+build.201 fabric_loader_version = 0.9.2+build.206
# Mod Properties # Mod Properties
maven_group = com.thebrokenrail maven_group = com.thebrokenrail
@ -13,4 +13,4 @@ org.gradle.jvmargs = -Xmx1G
# Dependencies # Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_api_version = 0.11.8+build.357-1.16 fabric_api_version = 0.19.0+build.398-1.16

View File

@ -0,0 +1,13 @@
package com.thebrokenrail.scriptcraft.api.bridge;
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
import net.minecraft.block.Block;
import net.minecraft.block.Material;
class BlockBridges {
static void register() {
ScriptCraftCore.addBridge("Block.getMaterial", args -> ((Block) args[0]).getDefaultState().getMaterial());
ScriptCraftCore.addBridge("Block.getMaterialColor", args -> ((Block) args[0]).getDefaultState().getTopMaterialColor(null, null));
ScriptCraftCore.addBridge("Block.isValid", args -> args[0] instanceof Block);
}
}

View File

@ -10,26 +10,14 @@ import java.util.Locale;
class BlockSettingsBridges { class BlockSettingsBridges {
static void register() { static void register() {
ScriptCraftCore.addBridge("BlockSettings.create", args -> { ScriptCraftCore.addBridge("BlockSettings.create", args -> {
try { Material material = (Material) args[0];
String materialID = ((String) args[0]).toUpperCase(Locale.ROOT); MaterialColor materialColor = (MaterialColor) args[1];
Material material = (Material) Material.class.getField(materialID).get(null);
MaterialColor materialColor; AbstractBlock.Settings settings = AbstractBlock.Settings.of(material, materialColor);
if (args[1] != null) {
String materialColorID = ((String) args[1]).toUpperCase(Locale.ROOT);
materialColor = (MaterialColor) MaterialColor.class.getField(materialColorID).get(null);
} else {
materialColor = material.getColor();
}
AbstractBlock.Settings settings = AbstractBlock.Settings.of(material, materialColor); settings.strength(((Double) args[2]).floatValue(), ((Double) args[3]).floatValue());
settings.strength(((Double) args[2]).floatValue(), ((Double) args[3]).floatValue()); return settings;
return settings;
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}); });
} }
} }

View File

@ -3,7 +3,7 @@ package com.thebrokenrail.scriptcraft.api.bridge;
public class Bridges { public class Bridges {
public static void init() { public static void init() {
BlockSettingsBridges.register(); BlockSettingsBridges.register();
RegistryBridge.register(); RegistryBridges.register();
ItemStackBridges.register(); ItemStackBridges.register();
LivingEntityBridges.register(); LivingEntityBridges.register();
BlockStateBridges.register(); BlockStateBridges.register();
@ -16,5 +16,9 @@ public class Bridges {
PlayerEntityBridges.register(); PlayerEntityBridges.register();
BlockEntityBridges.register(); BlockEntityBridges.register();
EventBridges.register(); EventBridges.register();
MaterialBridges.register();
MaterialColorBridges.register();
BlockBridges.register();
BuiltinRegistryBridges.register();
} }
} }

View File

@ -0,0 +1,11 @@
package com.thebrokenrail.scriptcraft.api.bridge;
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
class BuiltinRegistryBridges {
static void register() {
ScriptCraftCore.addBridge("BuiltinRegistry.getBlock", args -> Registry.BLOCK.get(new Identifier((String) args[0])));
}
}

View File

@ -5,21 +5,15 @@ import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import java.util.Locale;
class DamageSourceBridges { class DamageSourceBridges {
static void register() { static void register() {
ScriptCraftCore.addBridge("DamageSource.create", args -> { ScriptCraftCore.addBridge("DamageSource.getGeneric", args -> DamageSource.GENERIC);
try { ScriptCraftCore.addBridge("DamageSource.getOutOfWorld", args -> DamageSource.OUT_OF_WORLD);
String id = ((String) args[0]).toUpperCase(Locale.ROOT);
return DamageSource.class.getField(id).get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
});
ScriptCraftCore.addBridge("DamageSource.createFromPlayer", args -> DamageSource.player((PlayerEntity) args[1])); ScriptCraftCore.addBridge("DamageSource.createFromExplosion", args -> DamageSource.explosion((LivingEntity) args[0]));
ScriptCraftCore.addBridge("DamageSource.createFromMob", args -> DamageSource.mob((LivingEntity) args[1]));
ScriptCraftCore.addBridge("DamageSource.createFromPlayer", args -> DamageSource.player((PlayerEntity) args[0]));
ScriptCraftCore.addBridge("DamageSource.createFromMob", args -> DamageSource.mob((LivingEntity) args[0]));
ScriptCraftCore.addBridge("DamageSource.isValid", args -> args[0] instanceof DamageSource); ScriptCraftCore.addBridge("DamageSource.isValid", args -> args[0] instanceof DamageSource);
} }

View File

@ -0,0 +1,11 @@
package com.thebrokenrail.scriptcraft.api.bridge;
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
import net.minecraft.block.Material;
class MaterialBridges {
static void register() {
ScriptCraftCore.addBridge("Material.getColor", args -> ((Material) args[0]).getColor());
ScriptCraftCore.addBridge("Material.isValid", args -> args[0] instanceof Material);
}
}

View File

@ -0,0 +1,11 @@
package com.thebrokenrail.scriptcraft.api.bridge;
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
import net.minecraft.block.Material;
import net.minecraft.block.MaterialColor;
class MaterialColorBridges {
static void register() {
ScriptCraftCore.addBridge("MaterialColor.isValid", args -> args[0] instanceof MaterialColor);
}
}

View File

@ -12,7 +12,7 @@ import net.minecraft.item.Item;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
class RegistryBridge { class RegistryBridges {
static void register() { static void register() {
ScriptCraftCore.addBridge("Registry.registerBlock", args -> { ScriptCraftCore.addBridge("Registry.registerBlock", args -> {
Registry.register(Registry.BLOCK, new Identifier((String) args[0]), new CustomBlock((Block.Settings) args[1], new Identifier((String) args[0]))); Registry.register(Registry.BLOCK, new Identifier((String) args[0]), new CustomBlock((Block.Settings) args[1], new Identifier((String) args[0])));

View File

@ -1,16 +1,57 @@
import { World } from './world'; import { World } from './world';
import { PlayerEntity } from './entity'; import { PlayerEntity } from './entity';
import { Identifier, Hand, Pos, ActionResult, Direction, SimpleRegistry, JavaObjectWrapper } from './core'; import { Identifier, Hand, Pos, ActionResult, Direction, SimpleRegistry, JavaObjectWrapper, SimpleBuiltinRegistry } from './core';
import { CompoundTag } from './tag'; import { CompoundTag } from './tag';
import { useBridge, addBridge } from 'scriptcraft-core'; import { useBridge, addBridge } from 'scriptcraft-core';
import { Inventory } from './inventory'; import { Inventory } from './inventory';
/**
* Block Material
*/
class Material extends JavaObjectWrapper {
/**
* Create Material Wrapper From Java Object
* @param javaObject Java Object
*/
constructor(javaObject: JavaObject | JavaObjectWrapper) {
super(javaObject);
this.assertValidJavaObject('Material');
}
/**
* Get Associated Material Color
* @returns Material Color
*/
getColor(): MaterialColor {
const obj = useBridge('Material.getColor', this.getJavaObject()) as JavaObject;
if (obj) {
return new MaterialColor(obj);
} else {
return null;
}
}
}
/**
* Block Material Color
*/
class MaterialColor extends JavaObjectWrapper {
/**
* Create Material Color Wrapper From Java Object
* @param javaObject Java Object
*/
constructor(javaObject: JavaObject | JavaObjectWrapper) {
super(javaObject);
this.assertValidJavaObject('MaterialColor');
}
}
/** /**
* Settings for {@link CustomBlock} * Settings for {@link CustomBlock}
*/ */
export class BlockSettings { export class BlockSettings {
#material: string; #material: Material;
#materialColor: string; #materialColor: MaterialColor;
#resistance: number; #resistance: number;
#hardness: number; #hardness: number;
@ -20,11 +61,11 @@ export class BlockSettings {
* @param material Material * @param material Material
* @param materialColor Material Color * @param materialColor Material Color
*/ */
constructor(material: string); constructor(material: Material);
constructor(material: string, materialColor: string); constructor(material: Material, materialColor: MaterialColor);
constructor(material: string, materialColor?: string) { constructor(material: Material, materialColor?: MaterialColor) {
this.#material = material; this.#material = material;
this.#materialColor = materialColor; this.#materialColor = materialColor !== null ? materialColor : material.getColor();
this.#resistance = 0; this.#resistance = 0;
this.#hardness = 0; this.#hardness = 0;
@ -63,7 +104,7 @@ export class BlockSettings {
* Get Material * Get Material
* @returns Material * @returns Material
*/ */
getMaterial(): string { getMaterial(): Material {
return this.#material; return this.#material;
} }
@ -71,7 +112,7 @@ export class BlockSettings {
* Set Material * Set Material
* @param material Material * @param material Material
*/ */
setMaterial(material: string): BlockSettings { setMaterial(material: Material): BlockSettings {
this.#material = material; this.#material = material;
return this; return this;
} }
@ -80,7 +121,7 @@ export class BlockSettings {
* Set Material Color * Set Material Color
* @param materialColor Material Color * @param materialColor Material Color
*/ */
setMaterialColor(materialColor: string): BlockSettings { setMaterialColor(materialColor: MaterialColor): BlockSettings {
this.#materialColor = materialColor; this.#materialColor = materialColor;
return this; return this;
} }
@ -89,7 +130,7 @@ export class BlockSettings {
* Get Material Color * Get Material Color
* @returns Material Color * @returns Material Color
*/ */
getMaterialColor(): string { getMaterialColor(): MaterialColor {
return this.#materialColor; return this.#materialColor;
} }
@ -114,7 +155,7 @@ export class BlockSettings {
* @returns Java Object * @returns Java Object
*/ */
createJavaObject(): JavaObject { createJavaObject(): JavaObject {
return useBridge('BlockSettings.create', this.getMaterial(), this.getMaterialColor(), this.getHardness(), this.getResistance()) as JavaObject; return useBridge('BlockSettings.create', this.getMaterial().getJavaObject(), this.getMaterialColor().getJavaObject(), this.getHardness(), this.getResistance()) as JavaObject;
} }
} }
@ -337,6 +378,44 @@ export class CustomBlockWithEntity extends CustomBlock {
} }
} }
/**
* Built-In Block
*/
export class Block extends JavaObjectWrapper {
/**
* Create Built-In Block Wrapper From Java Object
* @param javaObject Java Object
*/
constructor(javaObject: JavaObject | JavaObjectWrapper) {
super(javaObject);
this.assertValidJavaObject('Block');
}
/**
* Get Block Material
*/
getMaterial(): Material {
const obj = useBridge('Block.getMaterial', this.getJavaObject()) as JavaObject;
if (obj) {
return new Material(obj);
} else {
return null;
}
}
/**
* Get Block MaterialColor
*/
getMaterialColor(): MaterialColor {
const obj = useBridge('Block.getMaterialColor', this.getJavaObject()) as JavaObject;
if (obj) {
return new MaterialColor(obj);
} else {
return null;
}
}
}
/** /**
* @internal * @internal
*/ */
@ -419,3 +498,22 @@ addBridge('CustomBlockEntity.free', (i: number) => {
addBridge('CustomBlock.onUse', (id: string, world: JavaObject, state: JavaObject, x: number, y: number, z: number, side: keyof typeof Direction, player: JavaObject, hand: keyof typeof Hand): string => { addBridge('CustomBlock.onUse', (id: string, world: JavaObject, state: JavaObject, x: number, y: number, z: number, side: keyof typeof Direction, player: JavaObject, hand: keyof typeof Hand): string => {
return BlockRegistry.INSTANCE.get(new Identifier(id)).onUse(new World(world), new BlockState(state), new Pos(x, y, z), Direction[side], new PlayerEntity(player), Hand[hand]); return BlockRegistry.INSTANCE.get(new Identifier(id)).onUse(new World(world), new BlockState(state), new Pos(x, y, z), Direction[side], new PlayerEntity(player), Hand[hand]);
}); });
/**
* @internal
*/
export class BlockBuiltinRegistry implements SimpleBuiltinRegistry<Block> {
static readonly INSTANCE = new BlockBuiltinRegistry();
private constructor() {
}
get(id: Identifier): Block {
const obj = useBridge('BuiltinRegistry.getBlock', id.toString()) as JavaObject;
if (obj) {
return new Block(obj);
} else {
return null;
}
}
}

View File

@ -0,0 +1,12 @@
import { Block, BlockBuiltinRegistry } from './block';
import { SimpleBuiltinRegistry } from './core';
/**
* Builtin Registry
*/
export class BuiltinRegistry {
/**
* Block Builtin Registry
*/
static BLOCK: SimpleBuiltinRegistry<Block> = BlockBuiltinRegistry.INSTANCE;
}

View File

@ -335,3 +335,15 @@ export interface SimpleRegistry<T> {
*/ */
getID(obj: T): Identifier; getID(obj: T): Identifier;
} }
/**
* Simple Builtin Registry
*/
export interface SimpleBuiltinRegistry<T> {
/**
* Get Object From ID
* @param id ID
* @returns Object
*/
get(id: Identifier): T;
}

View File

@ -5,7 +5,16 @@ import { CompoundTag } from './tag';
import { useBridge } from 'scriptcraft-core'; import { useBridge } from 'scriptcraft-core';
import { Inventory } from './inventory'; import { Inventory } from './inventory';
function buildDamageSource(bridge: string, value: BridgeValueType): DamageSource { function buildDamageSource(bridge: string): DamageSource {
const obj = useBridge(bridge) as JavaObject;
if (obj !== null) {
return new DamageSource(obj);
} else {
return null;
}
}
function buildDamageSourceWithValue(bridge: string, value: BridgeValueType): DamageSource {
const obj = useBridge(bridge, value) as JavaObject; const obj = useBridge(bridge, value) as JavaObject;
if (obj !== null) { if (obj !== null) {
return new DamageSource(obj); return new DamageSource(obj);
@ -18,6 +27,15 @@ function buildDamageSource(bridge: string, value: BridgeValueType): DamageSource
* Damage Source * Damage Source
*/ */
export class DamageSource extends JavaObjectWrapper { export class DamageSource extends JavaObjectWrapper {
/**
* Generic Damage Source
*/
static GENERIC = buildDamageSource('DamageSource.getGeneric');
/**
* Void Damage Source
*/
static OUT_OF_WORLD = buildDamageSource('DamageSource.getOutOfWorld');
/** /**
* Create Damage Source Wrapper From Java Object * Create Damage Source Wrapper From Java Object
* @param javaObject Java Object * @param javaObject Java Object
@ -28,12 +46,12 @@ export class DamageSource extends JavaObjectWrapper {
} }
/** /**
* Create Damage Source from type * Create Damage Source from an explosion
* @param value Type * @param value Explosion Source
* @returns Damage Source * @returns Damage Source
*/ */
static create(value: string): DamageSource { static createFromExplosion(value: LivingEntity): DamageSource {
return buildDamageSource('DamageSource.create', value); return buildDamageSourceWithValue('DamageSource.explosion', value.getJavaObject());
} }
/** /**
@ -42,7 +60,7 @@ export class DamageSource extends JavaObjectWrapper {
* @returns Damage Source * @returns Damage Source
*/ */
static createFromPlayer(value: PlayerEntity): DamageSource { static createFromPlayer(value: PlayerEntity): DamageSource {
return buildDamageSource('DamageSource.createFromPlayer', value.getJavaObject()); return buildDamageSourceWithValue('DamageSource.createFromPlayer', value.getJavaObject());
} }
/** /**
@ -51,7 +69,7 @@ export class DamageSource extends JavaObjectWrapper {
* @returns Damage Source * @returns Damage Source
*/ */
static createFromMob(value: LivingEntity): DamageSource { static createFromMob(value: LivingEntity): DamageSource {
return buildDamageSource('DamageSource.createFromMob', value.getJavaObject()); return buildDamageSourceWithValue('DamageSource.createFromMob', value.getJavaObject());
} }
} }

View File

@ -17,3 +17,4 @@ export { CompoundTag, ListTag, NumberType } from './tag';
export { Registry } from './registry'; export { Registry } from './registry';
export { Inventory } from './inventory'; export { Inventory } from './inventory';
export { Events, Event, BlockEvent, ItemEvent, WorldEvent } from './event'; export { Events, Event, BlockEvent, ItemEvent, WorldEvent } from './event';
export { BuiltinRegistry } from './builtin-registry';