Skip to content

Commit 7f0988d

Browse files
committed
修复了Entity.getNbt和setNbt的bug,添加了ItemStack
1 parent 4c1a84c commit 7f0988d

16 files changed

Lines changed: 400 additions & 41 deletions

jni/header/bluebox_ll_entity_Player.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jni/header/bluebox_ll_item_ItemStack.h

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jni/jni/ItemStack.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "../header/bluebox_ll_item_ItemStack.h"
2+
#include "../util/util.h"
3+
4+
#include "mc/world/item/registry/ItemStack.h"
5+
6+
/*
7+
* Class: bluebox_ll_item_ItemStack
8+
* Method: getCustomName
9+
* Signature: ()Ljava/lang/String;
10+
*/
11+
JNIEXPORT jstring JNICALL Java_bluebox_ll_item_ItemStack_getCustomName
12+
(JNIEnv *env, jobject thisObj){
13+
ItemStack* item=(ItemStack*)lje::getNativePointer(env, thisObj);
14+
return env->NewStringUTF(item->getCustomName().c_str());
15+
}
16+
17+
/*
18+
* Class: bluebox_ll_item_ItemStack
19+
* Method: getRawName
20+
* Signature: ()Ljava/lang/String;
21+
*/
22+
JNIEXPORT jstring JNICALL Java_bluebox_ll_item_ItemStack_getRawName
23+
(JNIEnv *env, jobject thisObj){
24+
ItemStack* item=(ItemStack*)lje::getNativePointer(env, thisObj);
25+
return env->NewStringUTF(item->getRawNameId().c_str());
26+
}
27+
28+
/*
29+
* Class: bluebox_ll_item_ItemStack
30+
* Method: getName
31+
* Signature: ()Ljava/lang/String;
32+
*/
33+
JNIEXPORT jstring JNICALL Java_bluebox_ll_item_ItemStack_getName
34+
(JNIEnv *env, jobject thisObj){
35+
ItemStack* item=(ItemStack*)lje::getNativePointer(env, thisObj);
36+
return env->NewStringUTF(item->getName().c_str());
37+
}
38+
39+
/*
40+
* Class: bluebox_ll_item_ItemStack
41+
* Method: setCustomName
42+
* Signature: (Ljava/lang/String;)V
43+
*/
44+
JNIEXPORT void JNICALL Java_bluebox_ll_item_ItemStack_setCustomName
45+
(JNIEnv *env, jobject thisObj, jstring jstr_name){
46+
ItemStack* item=(ItemStack*)lje::getNativePointer(env, thisObj);
47+
const char* name=env->GetStringUTFChars(jstr_name, NULL);
48+
item->setCustomName(name);
49+
env->ReleaseStringUTFChars(jstr_name, name);
50+
}
51+
52+
/*
53+
* Class: bluebox_ll_item_ItemStack
54+
* Method: getCount
55+
* Signature: ()I
56+
*/
57+
JNIEXPORT jint JNICALL Java_bluebox_ll_item_ItemStack_getCount
58+
(JNIEnv *env, jobject thisObj){
59+
ItemStack* item=(ItemStack*)lje::getNativePointer(env, thisObj);
60+
return item->mCount;
61+
}
62+
63+
/*
64+
* Class: bluebox_ll_item_ItemStack
65+
* Method: setCount
66+
* Signature: (I)V
67+
*/
68+
JNIEXPORT void JNICALL Java_bluebox_ll_item_ItemStack_setCount
69+
(JNIEnv *env, jobject thisObj, jint jint_count){
70+
ItemStack* item=(ItemStack*)lje::getNativePointer(env, thisObj);
71+
item->mCount=jint_count;
72+
}
73+
74+
/*
75+
* Class: bluebox_ll_item_ItemStack
76+
* Method: getTypeName
77+
* Signature: ()Ljava/lang/String;
78+
*/
79+
JNIEXPORT jstring JNICALL Java_bluebox_ll_item_ItemStack_getTypeName
80+
(JNIEnv *env, jobject thisObj){
81+
ItemStack* item=(ItemStack*)lje::getNativePointer(env, thisObj);
82+
return env->NewStringUTF(item->getTypeName().c_str());
83+
}

jni/jni/Player.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,15 @@ JNIEXPORT void JNICALL Java_bluebox_ll_entity_Player_sendChat
3131
packet.mMessage.assign(c_message);
3232
player->sendNetworkPacket(packet);
3333
env->ReleaseStringUTFChars(jstr_message,c_message);
34+
}
35+
36+
/*
37+
* Class: bluebox_ll_entity_Player
38+
* Method: updateInventory
39+
* Signature: ()V
40+
*/
41+
JNIEXPORT void JNICALL Java_bluebox_ll_entity_Player_updateInventory
42+
(JNIEnv *env, jobject thisObj){
43+
Player* player = (Player*)lje::getNativePointer(env, thisObj);
44+
player->refreshInventory();
3445
}

jni/util/nbt/java_nbt.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobject newTagCompound(JNIEnv* env, const CompoundTag* compound){
1616
jclass jclass_NbtCompound=env->FindClass(JCLASS_COMPOUND_TAG);
1717
jmethodID jmethod_NbtCompound_init=env->GetMethodID(jclass_NbtCompound,"<init>","()V");
1818
jobject jobject_native_compound=env->NewObject(jclass_NbtCompound,jmethod_NbtCompound_init);
19-
jmethodID jmethod_NbtCompound_put=env->GetMethodID(jclass_NbtCompound,"put","(Ljava/lang/String;" JCLASS_COMPOUND_TAG ")" JCLASS_COMPOUND_TAG );
19+
jmethodID jmethod_NbtCompound_put=env->GetMethodID(jclass_NbtCompound,"put","(" JCLASS_String JCLASS_TAG ")" JCLASS_TAG );
2020
for(auto it=compound->begin();it!=compound->end();++it){
2121
const std::string& key=it->first;
2222
const Tag& tag=it->second.get();
@@ -72,7 +72,7 @@ jobject newTagList(JNIEnv* env, const ListTag* list){
7272
jclass jclass_NbtList=env->FindClass(JCLASS_LIST_TAG);
7373
jmethodID jmethod_NbtList_init=env->GetMethodID(jclass_NbtList,"<init>","()V");
7474
jobject jobject_native_list=env->NewObject(jclass_NbtList,jmethod_NbtList_init);
75-
jmethodID jmethod_NbtList_add=env->GetMethodID(jclass_NbtList,"add","(" JCLASS_COMPOUND_TAG ")Z");
75+
jmethodID jmethod_NbtList_add=env->GetMethodID(jclass_NbtList,"add","(" JCLASS_TAG ")Z");
7676
for(auto it=list->begin();it!=list->end();++it){
7777
const Tag* tag=it->get();
7878
jobject jobject_native_note=nullptr;
@@ -158,7 +158,7 @@ jobject newTagDouble(JNIEnv* env, const DoubleTag* value){
158158
}
159159

160160
jobject newTagString(JNIEnv* env, const StringTag* value){
161-
jclass jclass_NbtString=env->FindClass(JCLASS_STRING_TAG);
161+
jclass jclass_NbtString=env->FindClass(JCLASS_String_TAG);
162162
jmethodID jmethod_NbtString_init=env->GetMethodID(jclass_NbtString,"<init>","(Ljava/lang/String;)V");
163163
jstring jstring_native_value=env->NewStringUTF(value->data());
164164
jobject jobject_native_value=env->NewObject(jclass_NbtString,jmethod_NbtString_init,jstring_native_value);
@@ -224,7 +224,7 @@ void toNativeDouble(JNIEnv *env, jobject obj, DoubleTag &tag){
224224
tag.data=(double)env->GetDoubleField(obj,jmethod_NbtDouble_value);
225225
}
226226
void toNativeString(JNIEnv *env, jobject obj, StringTag &tag){
227-
jclass jclass_NbtString=env->FindClass(JCLASS_STRING_TAG);
227+
jclass jclass_NbtString=env->FindClass(JCLASS_String_TAG);
228228
jfieldID jfield_NbtString_value=env->GetFieldID(jclass_NbtString,"value","Ljava/lang/String;");
229229
jstring jstring_value=(jstring)env->GetObjectField(obj,jfield_NbtString_value);
230230
const char* c_str_value=env->GetStringUTFChars(jstring_value,nullptr);
@@ -342,20 +342,23 @@ void toNativeList(JNIEnv *env, jobject thisObj, ListTag &tag){
342342

343343
void toNativeCompound(JNIEnv *env, jobject thisObj, CompoundTag &tag){
344344
jclass jclass_NbtCompound=env->FindClass(JCLASS_COMPOUND_TAG);
345+
jclass jclass_Iterator=env->FindClass("Ljava/util/Iterator;");
346+
jclass jclass_Set=env->FindClass("Ljava/util/Set;");
345347
jclass jclass_Nbt=env->FindClass(JCLASS_TAG);
346348
jmethodID jmethod_Nbt_getId=env->GetMethodID(jclass_Nbt,"getId","()I");
347349
jmethodID jmethod_NbtCompound_entrySet=env->GetMethodID(jclass_NbtCompound,"entrySet","()Ljava/util/Set;");
348350
jobject jobject_native_entries=env->CallObjectMethod(thisObj,jmethod_NbtCompound_entrySet);
349-
jclass jclass_NbtCompound_Entry=env->FindClass(PACK_JAVA_NAME("nbt/util/NbtCompound$Entry"));
350-
jmethodID jmethod_NbtCompound_Entry_getKey=env->GetMethodID(jclass_NbtCompound_Entry,"getKey","()Ljava/lang/String;");
351-
jmethodID jmethod_NbtCompound_Entry_getValue=env->GetMethodID(jclass_NbtCompound_Entry,"getValue","()" JCLASS_TAG);
352-
jmethodID jmethod_NbtCompound_Entry_iterator=env->GetMethodID(jclass_NbtCompound_Entry,"iterator","()Ljava/util/Iterator;");
353-
jmethodID jmethod_NbtCompound_Entry_hasNext=env->GetMethodID(jclass_NbtCompound_Entry,"hasNext","()Z");
354-
jmethodID jmethod_NbtCompound_Entry_next=env->GetMethodID(jclass_NbtCompound_Entry,"next","()Ljava/lang/Object;");
355-
jobject jobject_native_entry_iterator=env->CallObjectMethod(jobject_native_entries,jmethod_NbtCompound_Entry_iterator);
351+
jclass jclass_NbtCompound_Entry=env->FindClass("Ljava/util/Map$Entry;");
352+
jmethodID jmethod_NbtCompound_Entry_getKey=env->GetMethodID(jclass_NbtCompound_Entry,"getKey","()Ljava/lang/Object;");
353+
jmethodID jmethod_NbtCompound_Entry_getValue=env->GetMethodID(jclass_NbtCompound_Entry,"getValue","()Ljava/lang/Object;");
354+
jmethodID jmethod_Set_iterator=env->GetMethodID(jclass_Set,"iterator","()Ljava/util/Iterator;");
355+
env->ExceptionDescribe();
356+
jmethodID jmethod_NbtCompound_Iterator_hasNext=env->GetMethodID(jclass_Iterator,"hasNext","()Z");
357+
jmethodID jmethod_NbtCompound_Iterator_next=env->GetMethodID(jclass_Iterator,"next","()Ljava/lang/Object;");
358+
jobject jobject_native_entry_iterator=env->CallObjectMethod(jobject_native_entries,jmethod_Set_iterator);
356359

357-
while(env->CallBooleanMethod(jobject_native_entry_iterator,jmethod_NbtCompound_Entry_hasNext)){
358-
jobject jobject_native_entry=env->CallObjectMethod(jobject_native_entry_iterator,jmethod_NbtCompound_Entry_next);
360+
while(env->CallBooleanMethod(jobject_native_entry_iterator,jmethod_NbtCompound_Iterator_hasNext)){
361+
jobject jobject_native_entry=env->CallObjectMethod(jobject_native_entry_iterator,jmethod_NbtCompound_Iterator_next);
359362
jstring jstring_native_key=(jstring)env->CallObjectMethod(jobject_native_entry,jmethod_NbtCompound_Entry_getKey);
360363
jobject jobject_native_tag=(jobject)env->CallObjectMethod(jobject_native_entry,jmethod_NbtCompound_Entry_getValue);
361364
const char* c_str_key=nullptr;

jni/util/nbt/java_nbt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#define JCLASS_LONG_TAG PACK_JAVA_NAME("util/nbt/TagLong")
1515
#define JCLASS_FLOAT_TAG PACK_JAVA_NAME("util/nbt/TagFloat")
1616
#define JCLASS_DOUBLE_TAG PACK_JAVA_NAME("util/nbt/TagDouble")
17-
#define JCLASS_STRING_TAG PACK_JAVA_NAME("util/nbt/TagString")
17+
#define JCLASS_String_TAG PACK_JAVA_NAME("util/nbt/TagString")
1818
#define JCLASS_BYTEARRAY_TAG PACK_JAVA_NAME("util/nbt/TagByteArray")
1919
#define JCLASS_INTARRAY_TAG PACK_JAVA_NAME("util/nbt/TagIntArray")
2020
#define JCLASS_END_TAG PACK_JAVA_NAME("util/nbt/TagEnd")

src/mod/Entry.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Entry.h"
66
#include "PluginManager.h"
7+
#include "event/java_event.h"
78
#include "ll/api/Expected.h"
89
#include "ll/api/Logger.h"
910
#include "ll/api/mod/Mod.h"
@@ -20,7 +21,6 @@ LJE_Manager ljeManage;
2021
std::shared_ptr<LJE_Manager> pluginManager;
2122
std::unique_ptr<LJE> instance;
2223
JavaVM* jvm=nullptr;
23-
JavaVMInitArgs vm_args;
2424

2525
void detachCurrentThread(){
2626
jvm->DetachCurrentThread();
@@ -44,13 +44,14 @@ LJE_Manager& LJE::getPluginManager() { return *pluginManager; }
4444
bool LJE::load() {
4545
using namespace std;
4646

47-
//create java vm
4847
JNIEnv* env=nullptr;
48+
jvm=nullptr;
4949
std::string option = ("-Djava.class.path=" + getSelf().getModDir().string() + "/LeviLaminaJavaEngine.jar");
5050
option=ll::string_utils::replaceAll(option,"/","\\");
5151
getSelf().getLogger().info("JVM option: " + option);
5252

5353
JavaVMOption options[1];
54+
JavaVMInitArgs vm_args;
5455
options[0].optionString = (char*) option.c_str();
5556
vm_args.version = JNI_VERSION_1_8;
5657
vm_args.nOptions = 1;
@@ -59,13 +60,13 @@ bool LJE::load() {
5960

6061
auto status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
6162
if(status!=JNI_OK){
62-
getSelf().getLogger().error("Failed to create JVM");
63+
getSelf().getLogger().error("Failed to create JVM:"+std::to_string(status));
6364
return false;
6465
}
6566
getSelf().getLogger().info("JVM created");
6667

6768
//尝试加载类
68-
jclass jclass_LeviLamina = env->FindClass(JCLASS_LEVILAMINA);
69+
jclass jclass_LeviLamina = env->FindClass(JCLASS_LeviLamina);
6970
if(jclass_LeviLamina==nullptr){
7071
getSelf().getLogger().error("Failed to load LeviLamina class");
7172
env->ExceptionDescribe();
@@ -74,14 +75,14 @@ bool LJE::load() {
7475
}
7576

7677

77-
// 注册native方法
78-
//register_native_methods(env);
78+
// 注册native事件
79+
lje::event::registerNatives();
7980

8081
// 启动 LeviLamina 类
8182
jclass jclass_File=env->FindClass("Ljava/io/File;");
82-
jclass jclass_Logger=env->FindClass(JCLASS_LOGGER);
83+
jclass jclass_Logger=env->FindClass(JCLASS_Logger);
8384
jfieldID jfield_LeviLamina_modRootDir=env->GetStaticFieldID(jclass_LeviLamina, "modRootDir", "Ljava/io/File;");
84-
jfieldID jfield_LeviLamina_logger=env->GetStaticFieldID(jclass_LeviLamina, "logger", JCLASS_LOGGER);
85+
jfieldID jfield_LeviLamina_logger=env->GetStaticFieldID(jclass_LeviLamina, "logger", JCLASS_Logger);
8586
jmethodID jmethod_LeviLamina_init=env->GetStaticMethodID(jclass_LeviLamina, "init", "()V");
8687
jmethodID jmethod_LeviLamina_load=env->GetStaticMethodID(jclass_LeviLamina, "load", "()V");
8788
jmethodID jmethod_Logger_init=env->GetMethodID(jclass_Logger, "<init>", "(J)V");
@@ -112,7 +113,7 @@ bool LJE::load() {
112113
bool LJE::enable() {
113114
JNIEnv* env=getEnv();
114115

115-
jclass jclass_LeviLamina = env->FindClass(JCLASS_LEVILAMINA);
116+
jclass jclass_LeviLamina = env->FindClass(JCLASS_LeviLamina);
116117
jmethodID jmethod_enable=env->GetStaticMethodID(jclass_LeviLamina, "enable", "()V");
117118
env->CallStaticVoidMethod(jclass_LeviLamina, jmethod_enable);
118119

@@ -123,13 +124,22 @@ bool LJE::enable() {
123124

124125
bool LJE::disable() {
125126
JNIEnv* env=getEnv();
126-
127-
jclass jclass_LeviLamina = env->FindClass(JCLASS_LEVILAMINA);
127+
jclass jclass_LeviLamina = env->FindClass(JCLASS_LeviLamina);
128128
jmethodID jmethod_disable=env->GetStaticMethodID(jclass_LeviLamina, "disable", "()V");
129129
env->CallStaticVoidMethod(jclass_LeviLamina, jmethod_disable);
130-
130+
131131
detachCurrentThread();
132-
jvm->DestroyJavaVM();
132+
getSelf().getLogger().info("destroying JVM:"+std::to_string(jvm->DestroyJavaVM()));
133+
return true;
134+
}
135+
136+
bool LJE::unload(){
137+
auto& pluginManagerRegistry=ll::mod::ModManagerRegistry::getInstance();
138+
bool isSuccess=pluginManagerRegistry.eraseManager("jvm");
139+
if(!isSuccess){
140+
getSelf().getLogger().error("Failed to unload plugin manager");
141+
return false;
142+
}
133143

134144
return true;
135145
}

src/mod/Entry.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
#include "ll/api/mod/NativeMod.h"
55

66
#include "jni.h"
7-
8-
#define PACK_JAVA_NAME(name) "Lbluebox/ll/" name ";"
9-
#define JCLASS_STRING "Ljava/lang/String;"
10-
#define JCLASS_LEVILAMINA PACK_JAVA_NAME("plugin/LeviLamina")
11-
#define JCLASS_LOGGER PACK_JAVA_NAME("Logger")
7+
#include "util.h"
128

139
namespace lje {
1410

@@ -39,7 +35,7 @@ class LJE{
3935

4036
// TODO: Implement this method if you need to unload the mod.
4137
// /// @return True if the mod is unloaded successfully.
42-
// bool unload();
38+
bool unload();
4339

4440
private:
4541
ll::mod::NativeMod& mSelf;

0 commit comments

Comments
 (0)